[Tutor] tuple unpacking

Alan Gauld alan.gauld at yahoo.co.uk
Sat Nov 21 12:43:37 EST 2020


On 21/11/2020 15:02, Manprit Singh wrote:

> Example 1 :
>>>> a, b, c = (2, 3, 6)
> Example 2:
>>>> x, y, z = [3, 6, 5]
> Example 3:
>>>> a, b, c, d = (i for i in range(1, 8) if i%2 != 0)

> 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 ?

Yes.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list