iterable-unpacking

Unpacking generalizations

Unpacking generalizations Question: PEP 448 — Additional Unpacking Generalizations allowed: >>> LOL = [[1, 2], [‘three’]] >>> [*LOL[0], *LOL[1]] [1, 2, ‘three’] Alright! Goodbye itertools.chain. Never liked you much anyway. >>> [*L for L in LOL] File "<ipython-input-21-e86d2c09c33f>", line 1 [*L for L in LOL] ^ SyntaxError: iterable unpacking cannot be used in comprehension Oh. …

Total answers: 2

Unpacking tuples in a python list comprehension (cannot use the *-operator)

Unpacking tuples in a python list comprehension (cannot use the *-operator) Question: I am trying to create a list based on another list, with the same values repeated 3 times consecutively. At the moment, I am using: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> for i in range( len( my_list …

Total answers: 2

Python: Splat/unpack operator * in python cannot be used in an expression?

Python: Splat/unpack operator * in python cannot be used in an expression? Question: Does anybody know the reasoning as to why the unary (*) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File “<stdin>”, …

Total answers: 3

Getting only element from a single-element list in Python?

Getting only element from a single-element list in Python? Question: When a Python list is known to always contain a single item, is there a way to access it other than: mylist[0] You may ask, ‘Why would you want to?’. Curiosity alone. There seems to be an alternative way to do everything in Python. Asked …

Total answers: 3

Why is it valid to assign to an empty list but not to an empty tuple?

Why is it valid to assign to an empty list but not to an empty tuple? Question: This came up in a recent PyCon talk. The statement [] = [] does nothing meaningful, but it does not throw an exception either. I have the feeling this must be due to unpacking rules. You can do …

Total answers: 4

pandas apply function that returns multiple values to rows in pandas dataframe

pandas apply function that returns multiple values to rows in pandas dataframe Question: I have a dataframe with a timeindex and 3 columns containing the coordinates of a 3D vector: x y z ts 2014-05-15 10:38 0.120117 0.987305 0.116211 2014-05-15 10:39 0.117188 0.984375 0.122070 2014-05-15 10:40 0.119141 0.987305 0.119141 2014-05-15 10:41 0.116211 0.984375 0.120117 2014-05-15 …

Total answers: 7

Why can a dictionary be unpacked as a tuple?

Why can a dictionary be unpacked as a tuple? Question: Today, I saw one statement which didn’t throw an exception. Can anyone explain the theory behind it? >>> x, y = {‘a’: 2, ‘b’: 5} >>> x ‘a’ >>> y ‘b’ Asked By: nik_kgp || Source Answers: In Python, every iterable can be unpacked1: >>> …

Total answers: 5

What does *tuple and **dict mean in Python?

What does *tuple and **dict mean in Python? Question: As mentioned in PythonCookbook, * can be added before a tuple. What does * mean here? Chapter 1.18. Mapping Names to Sequence Elements: from collections import namedtuple Stock = namedtuple(‘Stock’, [‘name’, ‘shares’, ‘price’]) s = Stock(*rec) # here rec is an ordinary tuple, for example: rec …

Total answers: 1

Split list into different variables

Split list into different variables Question: I have a list like this: [(‘love’, ‘yes’, ‘no’), (‘valentine’, ‘no’, ‘yes’), (‘day’, ‘yes’,’yes’)] How do I split this list into three variables where each variable holds one tuple, i.e. var1 = (‘love’, ‘yes’, ‘no’) var2 = (‘valentine’, ‘no’, ‘yes’) var3 = (‘day’, ‘yes’,’yes’) Asked By: Eagle || Source …

Total answers: 2

How does swapping of members in tuples (a,b)=(b,a) work internally?

How does swapping of members in tuples (a,b)=(b,a) work internally? Question: In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a …

Total answers: 1