Python Forum
Assigning multiple values using tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning multiple values using tuple
#1
As a newbie, I am trying to understand the assigning multiple values using tuples. May I request one of you to help me here?

>>> a=('x','y','z')
>>> a
('x', 'y', 'z')
>>> a=(1,2,3)
>>> a
(1, 2, 3)
>>> a=(u,v,z)
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    a=(u,v,z)
NameError: name 'u' is not defined
2nd piece:
>>> a
(1, 2, 3)
>>> (u,v,z)=a
>>> u
1
>>> v
2
>>> z
3
>>> a
(1, 2, 3)
What is happening here exactly?
Reply
#2
https://docs.python.org/3/tutorial/datas...-sequences Wrote:The statement
t = 12345, 54321, 'hello!'
is an example of tuple packing: the values 12345, 54321 and 'hello!' are packed together in a tuple. The reverse operation is also possible:

>>> x, y, z = t
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
Reply
#3
Despite the fact, that this is directly from documentation ("Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence.") one can have less variables than elements in sequence:

>>> start, *middle, end = (1, 2, 3, 4, 5)
>>> start
1
>>> middle
[2, 3, 4]
>>> end
5
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 2,198 Jan-03-2024, 09:42 AM
Last Post: buran
  How to combine multiple column values into 1? cubangt 15 2,780 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Assigning a new value to variable uriel 1 1,587 Dec-04-2021, 02:59 PM
Last Post: Underscore
  Function - Return multiple values tester_V 10 4,430 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,474 Mar-09-2021, 12:24 PM
Last Post: KMV
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,793 Nov-04-2020, 11:26 AM
Last Post: Aggam
  assigning a variable :( gr3yali3n 0 1,309 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,870 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Assigning Column nunique values to another DataFrame column Pythonito 1 1,871 Jun-26-2020, 06:52 AM
Last Post: hussainmujtaba
  How to pass multiple values from one sample to nc variable? Baloch 0 1,857 Jun-01-2020, 09:27 PM
Last Post: Baloch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020