2

I need to change a list of strings into a list of integers how do i do this

i.e

('1', '1', '1', '1', '2') into (1,1,1,1,2).

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
tyim
  • 33
  • 1
  • 4
  • 2
    possible duplicate of [How to convert strings into integers in python?](http://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python). [This one](http://stackoverflow.com/questions/6396733/python-and-arrays) is also similar. Not to mention [these ones](http://stackoverflow.com/search?q=python+convert+list+of+strings+to+integers). Please use the search before you ask a new question. – Felix Kling Aug 17 '11 at 17:41
  • nitpick: those are tuples, not lists. – Wooble Aug 17 '11 at 17:42
  • @Wooble: covered in my answer now. i wanted to write it from the beginning, but you have to be fast when answering such easy questions ;) – flying sheep Aug 17 '11 at 17:55

4 Answers4

8

Use list comprehensions:

strtuple = ('1', '1', '1', '1', '2')
intlist = [int(s) for s in strtuple]

Stuff for completeness:

As your “list” is in truth a tuple, i.e. a immutable list, you would have to use a generator expression together with a tuple constructor to get another tuple back:

inttuple = tuple(int(s) for s in strtuple)

The “generator expression” i talk about looks like this when not wrapped in a constructor call, and returns a generator this way.

intgenerator = (int(s) for s in strtuple)
flying sheep
  • 8,475
  • 5
  • 56
  • 73
2

Use the map function.

vals = ('1', '1', '1', '1', '2')
result = tuple(map(int, vals))
print result

Output:

(1, 1, 1, 1, 2)

A performance comparison with the list comprehension:

from timeit import timeit
print timeit("map(int, vals)", "vals = '1', '2', '3', '4'")
print timeit("[int(s) for s in strlist]", "strlist = ('1', '1', '1', '1', '2')")

Output:

3.08675879197
4.08549801721

And with longer lists:

print timeit("map(int, vals)", "vals = tuple(map(str, range(10000)))", number = 1000)
print timeit("[int(s) for s in strlist]", "strlist = tuple(map(str, range(10000)))", number = 1000)

Output:

6.2849350965
7.36635214811

It appears that, (on my machine) in this case, the map approach is faster than the list comprehension.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • Definitely nice to see map mentioned and point out that there is more than one approach. With that said, list comprehension tends to be easier to read, more pythonic, and (generally, there are edge case exceptions) faster. – TimothyAWiseman Aug 17 '11 at 17:42
  • 1
    @TimothyAWiseman: Using built-in functions with `map` should be faster than list comprehension. So this is a perfect edge case ;) – Felix Kling Aug 17 '11 at 17:44
  • 1
    @TimothyAWiseman: `map` is faster in this case. See my update. – recursive Aug 17 '11 at 17:58
  • i think that newbies should learn the more pythonic and mightier approach first. – flying sheep Aug 17 '11 at 18:03
  • oh, and no offense: your time measuring sucks. you have to try it with different list sizes to get meaningful results; plus, for lists this tiny, the results have exactly no meaning. – flying sheep Aug 17 '11 at 18:06
  • 1
    No offense taken. The question was asked with a tiny list, so I figured it was a good representation. I'll include a longer list in an attempt to suck less. I disagree that they have no meaning though. That's the aggregate time over a million iterations, so it must be repeatable. – recursive Aug 17 '11 at 18:07
  • what i want to say: in case of such tiny datasets, speed is 1. no factor, and 2. misleading, as the overhead of whoknowswhat most likely has much more impact. also, i get different results with, and without braces and with longer and shorter variable names (“vals” is faster than “strlist”). you are right: map with builtins *is* faster, but who cares when it’s one of the four milliseconds. – flying sheep Aug 17 '11 at 18:11
  • @flying sheep: I wouldn't have mentioned the speed issue, other than to reply to the statement that comprehensions are usually faster. That may be true, but I wanted to point out that it wasn't in this case. – recursive Aug 17 '11 at 18:16
  • @recursive let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2590/discussion-between-flying-sheep-and-recursive) – flying sheep Aug 17 '11 at 21:25
  • 1
    Thanks for pointing out that it is faster in this case, you ahve an excellent point. I think I would still prefer the list comprehension for the readability, but always good to have hard data to work with when making the decision. – TimothyAWiseman Aug 18 '11 at 00:07
2
map(int, ls)

Where ls is your list of strings.

Stanislav Shabalin
  • 19,028
  • 3
  • 18
  • 18
0

You could use list comprehension which would look roughly like:

newList = [int(x) for x in oldList]
TimothyAWiseman
  • 14,385
  • 12
  • 40
  • 47