If I were trying to input a list as an argument to a function in Python, is there any way to retain the argument as a list or will it have to be passed in one element at a time?
If you pass the list as is, then it will stay as a list.
>>> def myfunc(*args):
for arg in args:
print(arg)
>>> sample_list = [1, 2, 3]
>>> myfunc(sample_list)
[1, 2, 3]
The function prints the list as one item.
However, if you use the 'splat' or 'unpack' operator *, the list can be passed as multiple arguments.
>>> myfunc(*sample_list)
1
2
3
This is not an answer but it's hard to make code in comments/
Probably you meant this:
def f(a, b):
print("f(%s, %s)" % (a, b))
l = [1, 2]
f(*l)
The syntax you are looking for is this:
my_list = ["arg1", "arg2", None, 4, "arg5"]
print(*my_list)
is equivalent to:
print("arg1", "arg2", None, 4, "arg5")
You get the object inside the function that you pass as argument. If your argument is a list, then you get a list inside the function, of course as one proper list and not as single variables.
def func1(my_list):
print(type(my_list), my_list)
func1([1, 2, "abc"])
# output: <type 'list'> [1, 2, "abc"]
However, you can use several ways to achieve something else. You can...
Pass single arguments when calling the function, but receive all of them as single tuple (immutable list).
def func2(*my_list): # the * means that this parameter collects
# all remaining positional arguments
print(type(my_list), my_list)
func2(1, 2, "abc")
# output: <type 'tuple'> (1, 2, "abc")
Unpack a list in the function call and receive the elements as single arguments.
def func2(arg1, arg2, arg3):
print(type(arg1), arg1, type(arg2), arg2, type(arg3), arg3)
func2(*[1, 2, "abc"]) # the * unpacks the list to single positional arguments
# output: <type 'int'> 1 <type 'int'> 2 <type 'str'> abc
Related
This question already has answers here:
What do the * (star) and ** (double star) operators mean in a function call?
(4 answers)
Closed 2 years ago.
I'm using itertools.chain to "flatten" a list of lists in this fashion:
uniqueCrossTabs = list(itertools.chain(*uniqueCrossTabs))
how is this different than saying:
uniqueCrossTabs = list(itertools.chain(uniqueCrossTabs))
* is the "splat" operator: It takes an iterable like a list as input, and expands it into actual positional arguments in the function call.
So if uniqueCrossTabs were [[1, 2], [3, 4]], then itertools.chain(*uniqueCrossTabs) is the same as saying itertools.chain([1, 2], [3, 4])
This is obviously different from passing in just uniqueCrossTabs. In your case, you have a list of lists that you wish to flatten; what itertools.chain() does is return an iterator over the concatenation of all the positional arguments you pass to it, where each positional argument is iterable in its own right.
In other words, you want to pass each list in uniqueCrossTabs as an argument to chain(), which will chain them together, but you don't have the lists in separate variables, so you use the * operator to expand the list of lists into several list arguments.
chain.from_iterable() is better-suited for this operation, as it assumes a single iterable of iterables to begin with. Your code then becomes simply:
uniqueCrossTabs = list(itertools.chain.from_iterable(uniqueCrossTabs))
It splits the sequence into separate arguments for the function call.
>>> def foo(a, b=None, c=None):
... print a, b, c
...
>>> foo([1, 2, 3])
[1, 2, 3] None None
>>> foo(*[1, 2, 3])
1 2 3
>>> def bar(*a):
... print a
...
>>> bar([1, 2, 3])
([1, 2, 3],)
>>> bar(*[1, 2, 3])
(1, 2, 3)
Just an alternative way of explaining the concept/using it.
import random
def arbitrary():
return [x for x in range(1, random.randint(3,10))]
a, b, *rest = arbitrary()
# a = 1
# b = 2
# rest = [3,4,5]
This question already has answers here:
What do the * (star) and ** (double star) operators mean in a function call?
(4 answers)
Closed 2 years ago.
I'm using itertools.chain to "flatten" a list of lists in this fashion:
uniqueCrossTabs = list(itertools.chain(*uniqueCrossTabs))
how is this different than saying:
uniqueCrossTabs = list(itertools.chain(uniqueCrossTabs))
* is the "splat" operator: It takes an iterable like a list as input, and expands it into actual positional arguments in the function call.
So if uniqueCrossTabs were [[1, 2], [3, 4]], then itertools.chain(*uniqueCrossTabs) is the same as saying itertools.chain([1, 2], [3, 4])
This is obviously different from passing in just uniqueCrossTabs. In your case, you have a list of lists that you wish to flatten; what itertools.chain() does is return an iterator over the concatenation of all the positional arguments you pass to it, where each positional argument is iterable in its own right.
In other words, you want to pass each list in uniqueCrossTabs as an argument to chain(), which will chain them together, but you don't have the lists in separate variables, so you use the * operator to expand the list of lists into several list arguments.
chain.from_iterable() is better-suited for this operation, as it assumes a single iterable of iterables to begin with. Your code then becomes simply:
uniqueCrossTabs = list(itertools.chain.from_iterable(uniqueCrossTabs))
It splits the sequence into separate arguments for the function call.
>>> def foo(a, b=None, c=None):
... print a, b, c
...
>>> foo([1, 2, 3])
[1, 2, 3] None None
>>> foo(*[1, 2, 3])
1 2 3
>>> def bar(*a):
... print a
...
>>> bar([1, 2, 3])
([1, 2, 3],)
>>> bar(*[1, 2, 3])
(1, 2, 3)
Just an alternative way of explaining the concept/using it.
import random
def arbitrary():
return [x for x in range(1, random.randint(3,10))]
a, b, *rest = arbitrary()
# a = 1
# b = 2
# rest = [3,4,5]
Consider a function defined as:
def fun(a, *args):
print(type(args), args)
When called, it packs the extra positional arguments as a tuple.
>>> fun(2, 3, 4)
<class 'tuple'> (3, 4)
I want to achieve a similar thing outside of function arguments. So, I thought I could achieve the same thing with extended iterable unpacking, but it always packs things as a list and never as a tuple:
# RHS is a tuple
>>> (a, *args) = (2, 3, 4)
>>> type(args)
<class 'list'>
>>> args
[3, 4] #but args is not a tuple!
Making it no different from:
# RHS is a list
>>> (a, *args) = [2, 3, 4]
>>> type(args)
<class 'list'>
>>> args
[3, 4]
I can understand that this is how it is proposed in the PEP as well.
I am asking if there is another way to achieve what I want.
Of course, I could convert args to tuple by later doing:
>>> args = tuple(args)
>>> args
(3, 4)
If this cannot be achieved directly during the assignment.
The closest way I know (that I'll admit, doesn't directly answer your question) is just to slice the tuple:
t = (2,3,4)
a, args = t[0], t[1:]
>>> type(args)
<class 'tuple'>
Yes, it's verbose, but at least it avoids the intermediate list.
There are some additional considerations here to keep in mind. The type of the RHS is not restricted to list or tuple. You can unpack an arbitrary iterable:
a, *args = itertools.repeat('hi', 3)
There is no good reason that args should be either type in particular. It makes sense that the result is a list since it should be able to accumulate an arbitrary number of elements, but there is no major relevant difference in the C code, at least in CPython.
Function arguments pretty much have to be a tuple because the function can return the packed object and a closure. You do not want to be able to modify args in this case:
def func(arg, *args):
assert args
def blah():
return args[0]
return args, blah
This is a bit contrived, but illustrates why args can be considered public and should not be mutable in a function.
All that being said, here is a general purpose method for getting the unpacking you want, regardless of the RHS type:
t = (2, 3, 4)
it = iter(t)
a, args = next(it), tuple(it)
This question already has answers here:
What do the * (star) and ** (double star) operators mean in a function call?
(4 answers)
Closed last month.
The python docs gives this code as the reverse operation of zip:
>>> x2, y2 = zip(*zipped)
In particular
zip() in conjunction with the * operator can be used to unzip a list.
Can someone explain to me how the * operator works in this case? As far as I understand, * is a binary operator and can be used for multiplication or shallow copy...neither of which seems to be the case here.
Although hammar's answer explains how the reversing works in the case of the zip() function, it may be useful to look at argument unpacking in a more general sense. Let's say we have a simple function which takes some arguments:
>>> def do_something(arg1, arg2, arg3):
... print 'arg1: %s' % arg1
... print 'arg2: %s' % arg2
... print 'arg3: %s' % arg3
...
>>> do_something(1, 2, 3)
arg1: 1
arg2: 2
arg3: 3
Instead of directly specifying the arguments, we can create a list (or tuple for that matter) to hold them, and then tell Python to unpack that list and use its contents as the arguments to the function:
>>> arguments = [42, 'insert value here', 3.14]
>>> do_something(*arguments)
arg1: 42
arg2: insert value here
arg3: 3.14
This behaves as normal if you don't have enough arguments (or too many):
>>> arguments = [42, 'insert value here']
>>> do_something(*arguments)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/blair/<ipython console> in <module>()
TypeError: do_something() takes exactly 3 arguments (2 given)
You can use the same construct when defining a function to accept any number of positional arguments. They are given to your function as a tuple:
>>> def show_args(*args):
... for index, value in enumerate(args):
... print 'Argument %d: %s' % (index, value)
...
>>> show_args(1, 2, 3)
Argument 0: 1
Argument 1: 2
Argument 2: 3
And of course you can combine the two techniques:
>>> show_args(*arguments)
Argument 0: 42
Argument 1: insert value here
You can do a similar thing with keyword arguments, using a double asterix (**) and a dictionary:
>>> def show_kwargs(**kwargs):
... for arg, value in kwargs.items():
... print '%s = %s' % (arg, value)
...
>>> show_kwargs(age=24, name='Blair')
age = 24
name = Blair
And, of course, you can pass keyword arguments through a dictionary:
>>> values = {'name': 'John', 'age': 17}
>>> show_kwargs(**values)
age = 17
name = John
It is perfectly acceptable to mix the two, and you can always have required arguments and optional extra arguments to a function:
>>> def mixed(required_arg, *args, **kwargs):
... print 'Required: %s' % required_arg
... if args:
... print 'Extra positional arguments: %s' % str(args)
... if kwargs:
... print 'Extra keyword arguments: %s' % kwargs
...
>>> mixed(1)
Required: 1
>>> mixed(1, 2, 3)
Required: 1
Extra positional arguments: (2, 3)
>>> mixed(1, 2, 3, test=True)
Required: 1
Extra positional arguments: (2, 3)
Extra keyword arguments: {'test': True}
>>> args = (2, 3, 4)
>>> kwargs = {'test': True, 'func': min}
>>> mixed(*args, **kwargs)
Required: 2
Extra positional arguments: (3, 4)
Extra keyword arguments: {'test': True, 'func': <built-in function min>}
If you are taking optional keyword arguments and you want to have default values, remember you are dealing with a dictionary and hence you can use its get() method with a default value to use if the key does not exist:
>>> def take_keywords(**kwargs):
... print 'Test mode: %s' % kwargs.get('test', False)
... print 'Combining function: %s' % kwargs.get('func', all)
...
>>> take_keywords()
Test mode: False
Combining function: <built-in function all>
>>> take_keywords(func=any)
Test mode: False
Combining function: <built-in function any>
zip(*zipped) means "feed each element of zipped as an argument to zip". zip is similar to transposing a matrix in that doing it again will leave you back where you started.
>>> a = [(1, 2, 3), (4, 5, 6)]
>>> b = zip(*a)
>>> b
[(1, 4), (2, 5), (3, 6)]
>>> zip(*b)
[(1, 2, 3), (4, 5, 6)]
When used like this, the * (asterisk, also know in some circles as the "splat" operator) is a signal to unpack arguments from a list. See http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists for a more complete definition with examples.
That's actually pretty simple once you really understand what zip() does.
The zip function takes several arguments (all of iterable type) and pair items from these iterables according to their respective positions.
For example, say we have two arguments ranked_athletes, rewards passed to zip, the function call zip(ranked_athletes, rewards) will:
pair athlete that ranked first (position i=0) with the first/best reward (position i=0)
it will move the the next element, i=1
pair the 2nd athlete with its reward, the 2nd from reward.
...
This will be repeated until there is either no more athlete or reward left. For example if we take the 100m at the 2016 olympics and zip the rewards we have:
ranked_athletes = ["Usain Bolt", "Justin Gatlin", "Andre De Grasse", "Yohan Blake"]
rewards = ["Gold medal", "Silver medal", "Bronze medal"]
zip(ranked_athletes, rewards)
Will return an iterator over the following tuples (pairs):
('Usain Bolt', 'Gold medal')
('Justin Gatlin', 'Silver medal')
('Andre De Grasse', 'Bronze medal')
Notice how Yohan Blake has no reward (because there are no more reward left in the rewards list).
The * operator allows to unpack a list, for example the list [1, 2] unpacks to 1, 2. It basically transform one object into many (as many as the size of the list). You can read more about this operator(s) here.
So if we combine these two, zip(*x) actually means: take this list of objects, unpack it to many objects and pair items from all these objects according to their indexes. It only make sense if the objects are iterable (like lists for example) otherwise the notion of index doesn't really make sense.
Here is what it looks like if you do it step by step:
>>> print(x) # x is a list of lists
[[1, 2, 3], ['a', 'b', 'c', 'd']]
>>> print(*x) # unpack x
[1, 2, 3] ['a', 'b', 'c', 'd']
>>> print(list(zip(*x))) # And pair items from the resulting lists
[(1, 'a'), (2, 'b'), (3, 'c')]
Note that in this case, if we call print(list(zip(x))) we will just pair items from x (which are 2 lists) with nothing (as there are no other iterable to pair them with):
[ ([1, 2, 3], ), (['a', 'b', 'c', 'd'], )]
^ ^
[1, 2, 3] is paired with nothing |
|
same for the 2nd item from x: ['a', 'b', 'c', 'd']
Another good way to understand how zip works is by implementing your own version, here is something that will do more or less the same job as zip but limited to the case of two lists (instead of many iterables):
def zip_two_lists(A, B):
shortest_list_size = min(len(A), len(B))
# We create empty pairs
pairs = [tuple() for _ in range(shortest_list_size)]
# And fill them with items from each iterable
# according to their the items index:
for index in range(shortest_list_size):
pairs[index] = (A[index], B[index])
return pairs
print(zip_two_lists(*x))
# Outputs: [(1, 'a'), (2, 'b'), (3, 'c')]
Notice how I didn't call print(list(zip_two_lists(*x))) that's because this function unlike the real zip isn't a generator (a function that constructs an iterator), but instead we create a list in memory. Therefore this function is not as good, you can find a better approximation to the real zip in Python's documentation. It's often a good idea to read these code equivalences you have all around this documentation, it's a good way to understand what a function does without any ambiguity.
I propose this one to unzip a zipped list of lists when zip is done with izip_longest:
>>> a =[2,3,4,5,6]
>>> b = [5,4,3,2]
>>> c=[1,0]]
>>>[list([val for val in k if val != None]) for k in
zip(*itertools.izip_longest(a,b,c))]
as izip_longest is appending None for lists shortest than the longest, I remove None beforehand. And I am back to the original a,b,c
[[2, 3, 4, 5, 6], [5, 4, 3, 2], [1, 0]]
I have an array that matches the parameters of a function:
TmpfieldNames = []
TmpfieldNames.append(Trademark.name)
TmpfieldNames.append(Trademark.id)
return func(Trademark.name, Trademark.id)
func(Trademark.name.Trademark.id) works, but func(TmpfieldNames) doesn't. How can I call the function without explicitly indexing into the array like func(TmpfieldNames[0], TmpfieldNames[1])?
With * you can unpack arguments from a list or tuple and ** unpacks arguments from a dict.
>>> range(3, 6) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]
Example from the documentation.
I think what you are looking for is this:
def f(a, b):
print a, b
arr = [1, 2]
f(*arr)
What you are looking for is:
func(*TmpfieldNames)
But this isn't the typical use case for such a feature; I'm assuming you've created it for demonstration.