HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonCritical

Convert all strings in a list to integers

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
allliststringsconvertintegers

Problem

How do I convert all strings in a list to integers?

['1', '2', '3']  ⟶  [1, 2, 3]

Solution

Given:

xs = ['1', '2', '3']


Use map then list to obtain a list of integers:

list(map(int, xs))


In Python 2, list was unnecessary since map returned a list:

map(int, xs)

Code Snippets

xs = ['1', '2', '3']
list(map(int, xs))
map(int, xs)

Context

Stack Overflow Q#7368789, score: 1525

Revisions (0)

No revisions yet.