Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python star unpacking for version 2.7

As mentioned here, you can use the star for unpacking an unknown number of variables (like in functions), but only in python 3:

>>> a, *b = (1, 2, 3) >>> b [2, 3] >>> a, *b = (1,) >>> b [] 

In python 2.7, the best I can come up with is (not terrible, but annoying):

c = (1, 2, 3) a, b = c[0], c[1:] if len(c) > 1 else [] 

Is there a way to import this from __future__ like division, or will I need my own function to do unknown-length unpacking in python 2.7?

like image 553
beardc Avatar asked May 29 '12 04:05

beardc


People also ask

Does * unpack list 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.

How do you unpack in Python?

Unpacking Dictionaries With the ** Operator In the context of unpacking in Python, the ** operator is called the dictionary unpacking operator. The use of this operator was extended by PEP 448. Now, we can use it in function calls, in comprehensions and generator expressions, and in displays.

What is the unpacking operator in Python?

Both * and ** are the operators that perform packing and unpacking in Python. The * operator (quite often associated with args) can be used with any iterable (such as a tuple, list and strings), whereas the ** operator, (quite often associated with kwargs) can only be used on dictionaries.

How do I unpack a string in Python?

Method : Using format() + * operator + values() The combination of above functions can be used to solve this problem. In this, we use format to map required value with braces in string. The * operator is used to unpack and assign. The values are extracted using values().


2 Answers

in python 2.X, you can do:

c = (1, 2, 3) a, b = c[0], c[1:] 

as long as c has at least one member it will work because if c only has 1 thing in it c[1:] is [].

You should probably make sure there is at least one thing in c though, or else c[0] will raise an exception.

You could do something like:

try:     c = tuple(c)     a, b = c[0], c[1:] except TypeError, IndexError:     # c is not iterable, or c is iterable, but it doesn't have any stuff in it.     # do something else     pass 
like image 89
Andbdrew Avatar answered Oct 10 '22 09:10

Andbdrew


(a,b) = (None, []) if not len(c) else (c[0], c[1:]) 

is also an option for handling the case where c is an empty sequence, although it won't distinguish between [None] and [] in terms as assignments to a, b. So use it with care, the try / except is probably best.

I can see no real difference between Python 3 and 2.7 when handling an empty container, but the nice thing about Python 3 here is it works with any iterable.

This works in 2.7 if you know c is a generator.

a,b = c.next(), c 

But the full beauty of unpacking seems to require Python 3.

like image 31
Pete Cacioppi Avatar answered Oct 10 '22 10:10

Pete Cacioppi