This question already has answers here:
How to extract parameters from a list and pass them to a function call [duplicate]
(3 answers)
Closed 7 years ago.
I have a parameter list which is just inputs separated by commas.
Now that I have a non-empty list, myList[], how do I turn the entries of L into a parameter list?
Example: if want
myList[0], myList[1], myList[2]..., myList[10]
How can I do that? Thank you! Is there anything similar to unpacking?
You can use unpacking with *.
f(*myList)
Related
This question already has answers here:
python order of elements in set
(2 answers)
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 26 days ago.
I have a list a:
a={4941, 4980, 3855, 4763, 4955}
I convert into a list:
b=list(a)
b now becomes [4980, 4955, 4763, 4941, 3855]
Sorry am I missing something? Eventhough set is an unordered structure why should it change the order ? Or is it the list function that is doing this?
Sorry if this is too naive a question! Many thanks.
This question already has answers here:
How to extract parameters from a list and pass them to a function call [duplicate]
(3 answers)
How to split list and pass them as separate parameter?
(4 answers)
Closed 1 year ago.
Assuming i have a method definition like this do_stuff(*arg).
I have a list of values, ['a','b',...]. How best can i pass the values of this list as individual arguments to the do_stuff method as do_stuff('a','b',...) ?
I've unsuccessfully tried using list comprehension - do_stuff([str(value) for value in some_list])
I am hoping i don't have to change do_stuff() to accept a list
Any pointers would be appreciated.
You can pass them using the "*" operator. So an example would be like this:
def multiplyfouritems(a,b,c,d):
return a*b*c*d
multiplyfouritems(*[1,2,3,4])
It's the same syntax, with the *:
do_stuff(*['a','b','c'])
This question already has answers here:
How do I gather user numerical input into a list in Python?
(2 answers)
Closed 3 years ago.
how we can input in list by using for loop?
I can just input list by single line input command.
nlist=list(input("enter your list items :"))
nlist=str(input("enter your list items separated by , :")).split(',')
This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 8 months ago.
If I have a function:
def run(time, message, time_span_pattern):
...
And a list like:
run_args = ['1s', '1 second alarm', <_sre.SRE_Pattern object at 0x100435680>]
How can I pass the list, as separate arguments, to run? Is there a builtin way to do this, or am I forced to reference each element individually and by index?
You're looking for:
run(*run_args)
This is explained in more detail in this StackOverflow answer about the star and double star operator
It's also covered in the python docs
This question already has answers here:
How do I prepend to a short python list?
(7 answers)
Closed 6 years ago.
Recently I was learning python,and I want to use the function that is opposite of append() function.How can I use the function insert an element in the first position of the list.Thank you!
Prepend doesn't exist, but you can simply use the insert method:
list.insert(0, element)