Unzip the list and quit at the same time
I have a long list of bites that need to be passed in integers to a function. I am currently doing the following:
my_function(int(list[0]), int(list[1]), int(list[2]), int(list[3])...)
But I know that I can make a much shorter function call by unpacking the list:
my_function(*list)
I was wondering if there is a way to combine int()
casting with an unboxing list *
, something like this:
my_function(*int(list)) #Doesn't work
source to share
Use a built-in method map
like
my_function(*map(int, list))
Alternatively, try using a list:
my_function(*[int(x) for x in list])
BTW:
Please do not use list
as a name for a local variable, this will hide the built-in method list
.
Typically used to add underscores to variable names that would otherwise hide built-in methods / keyword conflicts.
source to share