Python Central HubSign in

Python Programming / Python Tuple

Unpack the Tuple

Learn how to unpack the tuple in Python. The unpacking of the tuple is a very useful feature in Python. It allows us to assign the values of the tuple to the variables.

diagram unpacking is a shape match, and the star absorbs the rest mermaid
The left side of the assignment describes a shape, and Python checks the right side against it. Too many or too few values is an immediate error, which is the point -- it catches a changed return signature at the assignment rather than three lines later. One starred name may absorb everything the fixed names do not need.

Understanding the Tuple Unpacking

What you understand by unpacking the tuple? It is a process of assigning the values of the tuple to the variables. The unpacking of the tuple is a very useful feature in Python. It allows us to assign the values of the tuple to the variables.

Creating a Tuple

First, We need to learn how to create a tuple. We can create a tuple using the following syntax.

  1. Using the () brackets. You can create a tuple using the () brackets. The tuple elements are separated by the comma (,) operator. The following example shows how to create a tuple using the () brackets.

    Python
    data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
    print(data)

    Output:

    cmd
    C:\Users\username>python tuple.py
    ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
  2. Using the tuple() constructor. You can also create a tuple using the tuple() constructor. The following example shows how to create a tuple using the tuple() constructor.

    Python
    data = tuple(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
    print(data)

    Output:

    cmd
    C:\Users\username>python tuple_constructor.py
    ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
  3. using the , operator. You can also create a tuple using the , operator. The following example shows how to create a tuple using the , operator.

    Python
    data = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'
    print(data)

    Output:

    cmd
    C:\Users\username>python tuple_comma.py
    ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')

Unpacking the Tuple

Unpacking the tuple is a process of assigning the values of the tuple to the variables. The unpacking of the tuple is a very useful feature in Python. It allows us to assign the values of the tuple to the variables.

Python
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
a, b, c, d, e, f, g, h, i, j = data
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
print(j)

Output:

cmd
C:\Users\username>python tuple_unpack.py
a
b
c
d
e
f
g
h
i
j

In this example, we declare a tuple and assign it to the variable data. We then unpack the tuple and assign the values of the tuple to the variables a, b, c, d, e, f, g, h, i, j. We then print the values of the variables. The output shows that the values of the tuple are assigned to the variables.

Unpacking the Tuple using the Asterisk Operator

We can also unpack the tuple using the asterisk operator.

Python
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
a, b, c, *d = data
print(a)
print(b)
print(c)
print(d)

Output:

cmd
C:\Users\username>python tuple_unpack_asterisk.py
a
b
c
['d', 'e', 'f', 'g', 'h', 'i', 'j']

In this example, we declare a tuple and assign it to the variable data. We then unpack the tuple and assign the values of the tuple to the variables a, b, c, d. We then print the values of the variables. The output shows that the values of the tuple are assigned to the variables. * operator is used to unpack the tuple and assign the remaining values to the variable d.

Another Way

We can also unpack the tuple using the asterisk operator.

Python
data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
a,b,c,*d,e = data
print(a)
print(b)
print(c)
print(d)
print(e)

Output:

cmd
C:\Users\username>python tuple_unpack_asterisk_another_way.py
1
2
3
[4, 5, 6, 7, 8, 9]
10

In this example, we declare a tuple and assign it to the variable data. We then unpack the tuple and assign the values of the tuple to the variables a, b, c, d, e. We then print the values of the variables. The output shows that the values of the tuple are assigned to the variables. * operator is used to unpack the tuple and assign the remaining values to the variable d. The last value of the tuple is assigned to the variable e.

Unpacking the Nested Tuple

We can also unpack the nested tuple.

Python
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ('k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'))
a, b, c, d, e, f, g, h, i, j, k = data
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
print(j)
print(k)

Output:

cmd
C:\Users\username>python tuple_nested.py
a
b
c
d
e
f
g
h
i
j
('k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't')

In this example, we declare a tuple and assign it to the variable data. We then unpack the tuple and assign the values of the tuple to the variables a, b, c, d, e, f, g, h, i, j, k. We then print the values of the variables. The output shows that the values of the tuple are assigned to the variables. The last value of the tuple is a nested tuple. The nested tuple is assigned to the variable k.

Conclusion

In this tutorial, you learned how to unpack the tuple in Python. The unpacking of the tuple is a very useful feature in Python. It allows us to assign the values of the tuple to the variables. You can also read the official documentation. From now on, if you find any difficulty in unpacking the tuple, you can refer to this tutorial. If you like this tutorial, please share it with others via the social media platform. Happy Pythoning!. For more tutorials, you can visit Python Central Hub.


sketch Unpacking is a shape check, and the star absorbs the rest p5.js
The left-hand side describes a shape and Python checks the right-hand side against it. Too many or too few values raises immediately, at the assignment, rather than producing something subtly wrong further on. One starred name may absorb whatever the fixed names do not need -- and it always produces a list, even when there is nothing left.

Check yourself

quizCheck yourself
  1. `a, b = (1, 2, 3)`. What happens?

    Show answer

    B — `ValueError: too many values to unpack` — Verified — the message even reports the counts: expected 2, got 3. Unpacking is a shape check, and a mismatch is caught at the assignment rather than downstream.

  2. `a, *rest = (1,)`. What is `rest`?

    Show answer

    B — `[]` — A starred name always produces a LIST, and produces an empty one rather than being undefined. That means you never have to check whether it exists.

  3. What does `*init, last = (1, 2, 3)` bind?

    Show answer

    B — `init=[1,2], last=3` — The star may appear anywhere in the shape — the fixed names take their positions and the star absorbs the middle.

  4. Why is unpacking preferable to indexing a returned tuple?

    Show answer

    B — A changed return signature fails immediately, at the line that reads it — Indexing silently reads the wrong element when a function's shape changes. Unpacking raises `ValueError` right there, with the expected and actual counts.

Try it: Tuple Unpacking Exercises

Exercise 1 – Basic Unpacking

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Star Unpacking

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Swap with Tuple Unpacking

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did