Take the 2-minute tour ×

I have a long list of stings which need to be passed into a function as integers. What I am doing right now is:

my_function(int(list[0]), int(list[1]), int(list[2]), int(list[3])...)

But I know I can make a much shorter function call by unpacking the list:


I was wondering if there was a way to combine int() casting with list unpacking *, something like this:

my_function(*int(list))  #Doesn't work
share|improve this question
Your last line was very close tho' - you just need to make all the values in the list ints which is where the map comes in @Thorsten is showing the way. –  sotapme Feb 4 '13 at 16:16

2 Answers 2

up vote 7 down vote accepted

Use the built-in method map, e.g.

my_function(*map(int, list))

Alternatively, try list-comprehension:

my_function(*[int(x) for x in list])


Please don't use list as name for a local variable, this will hide the built-in method list.

It is common use to append an underscore for variable-names that would otherwise hide built-in methods / conflict with keywords.

share|improve this answer
Excellent. Didn't know about this, thanks! –  BloonsTowerDefence Feb 4 '13 at 16:13
This is one of the nice things in Python. When you learn about it, you say "Wow", and when you have to go back to a language that doesn't have such features, you say "#@+*%&". So be prepared for that. –  Thorsten Kranz Feb 4 '13 at 16:17

mapping is the answer:

map(int, my_list)
share|improve this answer

Your Answer


