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
votes
2 answers

PYTHON3 - How to use List Comprehension with a numpy array, to unpack a tuple of Lists, to avoid running for loop multiple times

In Short -> How to write this expression correctly?? -> [(self._mean,self._var,self._priors)] = [ ([X[y==c].mean(axis=0)] , [X[y==c].var(axis=0)],[X[y==c].shape[0] / n_samples ]) for c in self.classes] A Minimal Reproducable Example of this problem…
-1
votes
4 answers

extracting a list within a list in a tuple which happens to be in a pd.series

x= [[(some text,[a]), (some text,[b]), (some text,[c]).........]] [[(some text,[d]), (some text,[e]), (some text,[f]).........]] [[(some text,[g]), (some text,[h]), (some text,[k]).........]] [[(some text,[i]), (some text,[x]), (some…
-1
votes
4 answers

Don't understand this code showing tuple unpacking

Question: Could someone explain the output? Why is z equal to 2 in the second print()? Code: x=1 y=2 x,y,z=x,x,y print(y,x,y,z) z,y,z=x,y,z print(x,y,z) Output: 1 1 1 2 1 1 2
-1
votes
2 answers

How to find which line causes unpack error

I am facing a problem with the following error message in python. ValueError: not enough values to unpack (expected at least 2, got 1) a code line with the above error is op, param, *val = statement.split() I tried to find where a line has only…
KC Lee
  • 7
  • 2
-1
votes
1 answer

Why does unpacking of `*a, b = something` makes `a` always `list` type?

I was just testing around with unpacking, And I realized: *a, b = {1, 2, 3} Makes a a list, not a set. With it as: [1, 2] And I tried: *a, = {1, 2, 3} Expecting a set, but it still becomes a list as: [1, 2, 3] And even with tuples: *a, = (1, 2,…
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
-1
votes
2 answers

unpack list of lists in for loop not working

I have: master_list = [['001', '15\n', '963789', '40\n', '741239', '80\n', '985697', '80\n', '854698', '35\n', '965874', '10\n'], ['002', '25\n', '326574', '65\n', '944223', '40\n', '312689', '45\n', '225869', '80\n', '789654', '35\n'], ['003',…
Robert
  • 10,126
  • 19
  • 78
  • 130
-2
votes
4 answers

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

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 worked. Now I try to change…
Bryan Hunt
  • 3,685
  • 2
  • 24
  • 36
-2
votes
3 answers

Mapping tuple (R, R) into ((R,R),R)?

Input [[0 0 0 0 0] [0 4 0 0 0] [0 1 0 0 0] [0 1 2 0 0] [0 1 2 3 0]] Intended output [[(0, day00) (0, day01) (0, day02) (0, day03) (0, day04)] [(0, day10) (4, day11) (0, day12) (0, day13) (0, day14)] [(0, day20) (1, day21) (0, day22) (0,…
hhh
  • 50,788
  • 62
  • 179
  • 282
-2
votes
1 answer

How do you unpack a tuple of variable number of parameters (*args) inside a function?

What I have tried: def df(t,x,*system_constants): a,b,c,d,e,f,g,h,i,j,k,l,m = system_constants #unpack system constants #algebra with these constants to calculate something return something This returns an error when called,…
-2
votes
1 answer

Is cap.read() a tuple in OpenCV in Python?

How is cap.read() unpacked between ret and frame in import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() I have found sources which say that cap.read() returns a Boolean but how is that able to unpack between two…
-2
votes
2 answers

Building a python function that modifies lists with tuples

Hi guys I'm having the current problem: list_a = [('abc d',1), ('abc d',2) ,('acb e',3) ,('b',1),('b',2),('b',3)] from list_a, I am trying to build a function that would return the following output... Essentially I want to keep all [0] values that…
-2
votes
1 answer

why is my for loop printing 2 at each iteration if b has not even been defined as a placeholder in the for loop declaration?

I am fairly new to python and I just learned about tuple unpacking, so I was playing around with this concept and got a weird result. nList = [(1,2),4,5,6] for a in nList: print(a) print(b) I was expecting my program to crash since b is not…
-2
votes
2 answers

There is not enough values to unpack, how do I fix it?

I keep getting ValueError: not enough values to unpack (expected 4, got 1). Is this an issue of balancing both sides of the equal sign, and if so, how do I balance it? from sys import argv script, first, second, third = argv print("The script is…
C.M.
  • 9
  • 2
-2
votes
2 answers

Creating a tuple out of *args

I've tried, but I can't figure this out so far. I want to create a list of tuples, each one built out of dictionary values: my_list = [(x['field1'], x['field2']) for x in my_dict] But the issue is that I want to do this inside a function, passing…
-2
votes
1 answer

Why and where is this IndexError occurring?

I am trying to do a check on a position to see if the positions around it are both valid indexes as well as visually next to each other on a grid system(hence the count_neighbor function name). On line 8, I am getting an IndexError meaning the if…
Jordan
  • 902
  • 2
  • 10
  • 37
1 2 3
31
32