Skip to content

Unpack the Tuple

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.

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.

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.

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

    Output:

    command
    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.

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

    Output:

    command
    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.

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

    Output:

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

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.

tuple_unpack.py
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:

command
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

Section titled “Unpacking the Tuple using the Asterisk Operator”

We can also unpack the tuple using the asterisk operator.

tuple_unpack_asterisk.py
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:

command
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.

We can also unpack the tuple using the asterisk operator.

tuple_unpack_asterisk_another_way.py
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:

command
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.

We can also unpack the nested tuple.

tuple_nested.py
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:

command
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.

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.
pch.quizTag pch.quizDefaultTitle
  1. `a, b = (1, 2, 3)`. What happens?

    pch.quizShowAnswer

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

    pch.quizShowAnswer

    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?

    pch.quizShowAnswer

    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?

    pch.quizShowAnswer

    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.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading