Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Tuple Assignment, Packing, and Unpacking

Give Feedback

In this lesson, you’ll learn about tuple assignment, packing, and unpacking. A literal tuple containing several items can be assigned to a single object. Assigning the packed object to a new tuple unpacks the individual items into the objects in the new tuple:

>>>
>>> t = ('spam', 'egg', 'bacon', 'tomato')
>>> t
('spam', 'egg', 'bacon', 'tomato')
>>> t[0]
'spam'
>>> t[1]
'egg'

>>> (s1, s2, s3, s4) = t
>>> s1
'spam'
>>> s2
'egg'
>>> s3
'bacon'
>>> s4
'tomato'

>>> (s1, s2, s3) = t
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    (s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)

>>> t
('spam', 'egg', 'bacon', 'tomato')
>>> (s1, s2, s3, s4, s5) = t
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    (s1, s2, s3, s4, s5) = t
ValueError: not enough values to unpack (expected 5, got 4)

>>> (s1, s2, s3, s4) = ('spam', 'egg', 'bacon', 'tomato')
>>> s1
'spam'
>>> s2
'egg'
>>> s3
'bacon'
>>> s4
'tomato'

>>> (s1, s2, s3) = ('spam', 'egg', 'bacon', 'tomato')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    (s1, s2, s3) = t
ValueError: too many values to unpack (expected 3)

>>> t = 1, 2, 3
>>> t
(1, 2, 3)
>>> type(t)
<class 'tuple'>

>>> x1, x2, x3 = t
>>> x1, x2, x3
(1, 2, 3)
>>> x1, x2, x3 = 4, 5, 6
>>> x1, x2, x3
(4, 5, 6)

>>> t = 2,
>>> t
(2,)
>>> type(t)
<class 'tuple'>

>>> a = 'spam'
>>> b = 'egg'
>>> a, b
('spam', 'egg')
>>> # You need to define a temp variable to accomplish the swap.
>>> temp = a
>>> a = b
>>> b = temp
>>> a, b
('egg', 'spam')

>>> a = 'spam'
>>> b = 'egg'
>>> a, b
('spam', 'egg')
>>> # Ready for Magic Time!
>>> a, b = b, a
>>> a, b
('egg', 'spam')

>>> # Fibonacci series
>>> # the sum of two elements defines the next
>>> a, b = 0, 1
>>> while a < 30:
...    print(a)
...    a, b = b, a+b
...
0
1
1
2
3
5
8
13
21

reb24 on May 8, 2020

If I run this:

a = "spam"
b = "egg"

a, b => spam, egg

a, b = b, a
a, b => egg, spam

I can’t see any magic happening or reason to use temp variable to accomplish the swap

Dan Bader RP Team on May 8, 2020

The “magic” is that with Python you can do the swap without using an extra temporary variable. Many other programming languages would require the use of a temporary variable. Hope that clarifies it :)

reb24 on May 8, 2020

Ah Ok !!! Thanks Dan

kiran on July 25, 2020

Multiple Assignment (n, m = 300, 400) also called Tuple packing & unpacking?

Source: Multiple Assignment

Become a Member to join the conversation.