1

Possible Duplicate:
What is the easiest way to convert list with str into list with int? =)

Is it possible to transform:

a = ['1', '2', '3', '4']

to

a = [1, 2, 3, 4]

Thank You!

Community
  • 1
  • 1
Bob
  • 10,427
  • 24
  • 63
  • 71

2 Answers2

2

You could use map to apply a function to each element of a list, and a get the resulting list (Python 2.x) / iterable (Python 3.x) back.

map(int, a)

It could be done with list comprehension too.

[int(x) for x in a]
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

Another way:

result = [int(x) for x in a]

This is called a list comprehension.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452