iterable-unpacking

How to extract dictionary single key-value pair in variables

How to extract dictionary single key-value pair in variables Question: I have only a single key-value pair in a dictionary. I want to assign key to one variable and it’s value to another variable. I have tried with below ways but I am getting error for same. >>> d = {“a”: 1} >>> d.items() [(‘a’, …

Total answers: 11

Is it possible to return two lists from a function in python

Is it possible to return two lists from a function in python Question: I am new to python programming and need your help for the following: I want to return two lists from a function in python. How can i do that. And how to read them in the main program. Examples and illustrations would …

Total answers: 2

How are tuples unpacked in for loops?

How are tuples unpacked in for loops? Question: I stumbled across the following code: for i, a in enumerate(attributes): labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W)) e = Entry(root) e.grid(column=1, row=i) entries.append(e) entries[i].insert(INSERT,"text to insert") I don’t understand the i, a bit, and searching for information on for didn’t yield any useful results. …

Total answers: 8

Python star unpacking for version 2.7

Python star unpacking for version 2.7 Question: 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 …

Total answers: 3

Semantics of tuple unpacking in python

Semantics of tuple unpacking in python Question: Why does python only allow named arguments to follow a tuple unpacking expression in a function call? >>> def f(a,b,c): … print a, b, c … >>> f(*(1,2),3) File “<stdin>”, line 1 SyntaxError: only named arguments may follow *expression Is it simply an aesthetic choice, or are there …

Total answers: 6

How to unpack tuple of length n to m<n variables

How to unpack tuple of length n to m<n variables Question: In Python 3 I can do the following (see also PEP3132 on Extended Iterable Unpacking): a, *b = (1, 2, 3) # a = 1; b = (2, 3) What can I do to achieve the same similarly elegant in Python 2.x? I know …

Total answers: 5

Python, is this a bug, append to a list within a tuple results in None?

Python, is this a bug, append to a list within a tuple results in None? Question: This is one of the shortest examples I’ve written in a long time I create and update a tuple3 In [65]: arf=(0,1,[1,2,3]) In [66]: arf=(arf[0],arf[1], arf[2] ) In [67]: arf Out[67]: (0, 1, [1, 2, 3]) So the reassignment …

Total answers: 4

Ignore part of a python tuple

Ignore part of a python tuple Question: If I have a tuple such as (1,2,3,4) and I want to assign 1 and 3 to variables a and b I could obviously say myTuple = (1,2,3,4) a = myTuple[0] b = myTuple[2] Or something like (a,_,b,_) = myTuple Is there a way I could unpack the …

Total answers: 4

Unpacking, extended unpacking and nested extended unpacking

Unpacking, extended unpacking and nested extended unpacking Question: Consider the following expressions. Note that some expressions are repeated to present the "context". (this is a long list) a, b = 1, 2 # simple sequence assignment a, b = [‘green’, ‘blue’] # list asqignment a, b = ‘XY’ # string assignment a, b = range(1,5,2) …

Total answers: 4

Is it possible to unpack a tuple in Python without creating unwanted variables?

Is it possible to unpack a tuple in Python without creating unwanted variables? Question: Is there a way to write the following function so that my IDE doesn’t complain that column is an unused variable? def get_selected_index(self): (path, column) = self._tree_view.get_cursor() return path[0] In this case I don’t care about the second item in the …

Total answers: 4