Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: starred expression to unpack a list

Example use:

def f(a, b, c, d): 
    print(a, b, c, d, sep = '&')

f(1,2,3,4)
>>> 1&2&3&4

f(*[1, 2, 3, 4])
>>> 1&2&3&4

Where in the python documentation is * explained?

like image 698
Kifsif Avatar asked Sep 23 '12 19:09

Kifsif


People also ask

How do I unpack a list in Python?

Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.

When to use starred expression in Python?

The starred expression will create a list object, although we start with a tuple object. Starred expressions can be used with more than just tuples, and we can apply this technique for other iterables (e.g., lists, strings).

How to unpack a list in Python?

Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list. Did you find this tutorial helpful ?

Can I use multiple starred expressions in an unpacking assignment statement?

But it is possible to use multiple starred expressions in an unpacking assignment statement, as long as they’re catch-alls for different parts of the multilevel structure being unpacked. Starred expressions become list instances in all cases.

How to use starred expressions in Python?

Using starred expressions, first print the sum of the first values from each tuple in a raw_data list. Then, do the same with all values between the first and the last in each tuple. Now, you're fully starred prepped to use starred expressions! Hope you enjoyed my post!

What is sequence unpacking in Python and how to use it?

However, Python provides a better way to do this. It’s called sequence unpacking. Basically, you can assign elements of a list (and also a tuple) to multiple variables. For example: This statement assigns the first, second, and third elements of the colors list to the red, blue, and green variables.


2 Answers

The *args calling convention is documented in the Expressions reference:

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

So, since you used [1, 2, 3, 4] as the expression, which is an iterable, and there were no other positional arguments, it is treated as a call with M=0 and N=4, for a total of 4 positional arguments.

You can thus also call your function as f(1, 2, *[3, 4]) or any other combination of iterable and positional arguments, provided the iterable comes after the positionals.

like image 196
Martijn Pieters Avatar answered Oct 07 '22 20:10

Martijn Pieters


Just an addition to very simply expand on the combination of unnamed and named arguments.

This is the general order you want to keep in mind:

def func(arg_1, ..., arg_N, *args, kwarg_1, ..., kwarg_M, **kwargs):
    # do stuff
    return True

Where, in most typical cases;

  • each single arg_i is an unnamed argument,
  • args is a list, hence a set of unnamed arguments,
  • each single kwarg_j is a named argument,
  • kwargs is a dictionary, hence a set of named arguments.
like image 20
s.k Avatar answered Oct 07 '22 18:10

s.k