Questions tagged [iterable-unpacking]

A Python feature in which elements of an iterable are simultaneously assigned to multiple variables, e.g. a, b, c = [1, 2, 3].

Iterable unpacking (sometimes known as 'tuple unpacking', although the concept applies equally to any iterable, not just tuples) is a feature of Python which allows for the assignment of the elements of an iterable to be assigned to multiple variables:

>>> a, b, c = [1, 2, 3]
>>> a
1
>>> b
2
>>> c
3

This feature can be used to swap the values of two variables without the use of a temporary 'holding' variable, as traditionally employed in other languages:

>>> a = 1
>>> b = 2
>>> a, b = b, a
>>> a
2
>>> b
1
>>> # rather than:
... a = 1
>>> b = 2
>>> temp = a
>>> a = b
>>> b = temp
>>> a
2
>>> b
1

If the number of elements in the iterable does not match the number of variables on the left hand side of the assignment, A ValueError is raised:

>>> d, e = 4, 5, 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>> f, g, h = 7, 8
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

Since Python 3, Extended iterable unpacking allows "spare" elements of the iterable to be assigned to a list (note the *):

>>> x, *y, z = "kapow"
>>> x
'k'
>>> y
['a', 'p', 'o']
>>> z
'w'
469 questions
1
vote
1 answer

"Unpacking" in C

I am working on rewriting a script from python to C. I'm relatively new to C. I have a variable in PYTHON which contains this values: x = [chr(113),chr(80),chr(191),chr(70)] y = "".join(x) This will return this value of y: y = qP¿F #this…
Enzo Vidal
  • 249
  • 1
  • 5
  • 13
1
vote
2 answers

Most pythonic way to 'or' tuples?

I have a method that returns a three element tuple of boolean values and I call it on a loop. I want to finally get a three element tuple containing the or result of the individual tuples. If the method only returned a boolean it would just…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
1
vote
1 answer

Extracts the element of a tuple by type and ambiguity

The article states that for free std::get function overloadings (from 4-6 items) they Extracts the element of the tuple t whose type is T. Fails to compile if the tuple has more than one element of that type. Is the last statement a requirements…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
1
vote
0 answers

Why is `[] = []` a valid statement in Python?

I was surprised to learn that [] = [] is a perfectly valid Python statement (with no effect). Also: [] = tuple() [] = set() [] = {} I gather it's trying to unpack the iterables on the right hand side, but what's an iterable doing on the left hand…
Eli Rose
  • 6,788
  • 8
  • 35
  • 55
1
vote
3 answers

Map not unpacking tuples

I have this simple formula that converts an IP to a 32 bits integer: (first octet * 256**3) + (second octet * 256**2) + (third octet * 256**1) + (fourth octet) I made a program that does that: def ip_to_int32(ip): # split values ip =…
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
1
vote
1 answer

Why does running rake gems:unpack result in a Gem::FilePermissionError

I'm attempting to upgrade the friendly_id gem in a rails project. I have removed the old gem from the vendor directory, installed the new gem from rubygems.org. When I type: rake gems:unpack I get the following response: ERROR: While executing gem…
ktec
  • 2,453
  • 3
  • 26
  • 32
1
vote
1 answer

Trying to define a 6 dimensional variable and I get too many values to unpack error

I am trying to use gurobi library in python (gurobi is an optimization library ) I got this error ---- Value Error: too many values to unpack I'm trying to define a 6 dimensional variable in python. I defined each dimension as a list the dimensions…
Zed
  • 23
  • 1
  • 5
1
vote
2 answers

Need help understanding python syntax

Can someone explain the syntax on lines 5 & 16 1 # Using the generator pattern (an iterable) 2 class firstn(object): 3 def __init__(self, n): 4 self.n = n 5 self.num, self.nums = 0, [] 6 7 def…
Nutkin
  • 111
  • 1
1
vote
3 answers

Passing function returns as method parameters in Python

I'm currently writing a small code to move two balls around in a Tkinter GUI and do some other stuff too. I've already written a code that works, but as it uses a lot of global variables I tried to improve it. Below I've pasted the code section…
Pierre Monico
  • 982
  • 8
  • 16
1
vote
1 answer

finding the missing value for ValueError: need more than X values to unpack

I have a function call that looks like this: a,b,c,x,y,z = generatevalues(q) Its in a try block to catch the error but I also need to find out which value is missing. I can't clear the variables beforehand either. I'd also rather not merge the 6…
1
vote
1 answer

How do you include newlines in a python list unpack?

for row in rows: a, b, c = row is nice but for row in rows: alpha, beta, charlie, delta, echo, foxtrot, gamma, horseshoe, indigo, jimmy, killshot = row is not very nice. Python is usually good about supporting newlines after commas but I…
Nathan Spears
  • 1,657
  • 8
  • 23
  • 31
1
vote
2 answers

Maven dependencies jar not usable

EDIT: I am basically running into the following documented issue. I am using the maven assembly plugin to generate a jar file that includes my dependencies so that my project can be run from a single file. However, the jar file is not being built…
user130532
0
votes
2 answers

Squashing tuples from (a,(b,c)) to (a,b,c) in fsharp

Does it make sense to have such functions defined let squash12 (e:('a*('b*'c) )) = e |> (fun (a,(b,c) ) -> (a,b,c )) let squash21 (e:(('a*'b)*'c )) = e |> (fun ((a,b),c ) -> (a,b,c )) let squash13 (e:('a*('b*'c*'d))) = e |> (fun (a,(b,c,d))…
nicolas
  • 9,549
  • 3
  • 39
  • 83
0
votes
1 answer

calling row data from successive months in successive yrs and writing it into columns?

I have spread sheets of climate data for which, essentially, I need to transpose parts of rows into columns and vice versa. Unfortunately, the format is somewhat awkward. The data came to me with columns for year, month, number of days in the month,…
hdevs
  • 63
  • 1
  • 1
  • 4
0
votes
1 answer

How can I ungroup a polars dataframe in python?

I have a polars dataframe that has a particular column with repeating patterns. I have grouped them by the patterns & adding a new column to this grouped dataframe. But now I have to unpack/ungroup this dataframe. How can I do it in polars? My…