[Tutor] tuple unpacking

Manprit Singh manpritsinghece at gmail.com
Sat Nov 21 10:02:04 EST 2020


Dear sir ,

I just need to know the definition of tuple unpacking.  need to know if the
examples that i am writing below belongs to tuple unpacking or not :

Example 1 :
>>> a, b, c = (2, 3, 6)
>>> a
2
>>> b
3
>>> c
6
>>>

Example 2:
>>> x, y, z = [3, 6, 5]
>>> x
3
>>> y
6
>>> z
5
>>>

Example 3:
>>> a, b, c, d = (i for i in range(1, 8) if i%2 != 0)
>>> a
1
>>> b
3
>>> c
5
>>> d
7
>>>

Similarly values in set and dicts keys can be assigned to variables written
on the left side of the assignment statement , provided the number of
variables on the left of the assignment statement  must be equal to the
iterable on right side .

According to me these are all examples of tuple unpacking because variables
written on the left side of the assignment statement in above given
examples -     a, b, c are nothing but a tuple, and we are unpacking the
values of iterable on the right side to  it

is my understanding correct ?

Regards
Manprit Singh


More information about the Tutor mailing list