Unpack the Tuple
flowchart TD A["a, b = (1, 2)"] --> B["exact match -- fine"] C["a, b = (1, 2, 3)"] --> D["ValueError: too many values to unpack"] E["a, b, c = (1, 2)"] --> F["ValueError: not enough values to unpack"] G["a, *rest = (1, 2, 3, 4)"] --> H["a = 1, rest = [2, 3, 4] -- a LIST, always"] I["*init, last = (1, 2, 3)"] --> J["init = [1, 2], last = 3"] K["a, (b, c) = (1, (2, 3))"] --> L["nested shapes match too"] H --> M["rest is [] when there is nothing left, never absent"]
Understanding the Tuple Unpacking
Section titled “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
Section titled “Creating a Tuple”First, We need to learn how to create a tuple. We can create a tuple using the following syntax.
-
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') -
Using the
tuple()constructor. You can also create a tuple using thetuple()constructor. The following example shows how to create a tuple using thetuple()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') -
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
Section titled “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.
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:
C:\Users\username>python tuple_unpack.py
a
b
c
d
e
f
g
h
i
jIn 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.
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:
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
Section titled “Another Way”We can also unpack the tuple using the asterisk operator.
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:
C:\Users\username>python tuple_unpack_asterisk_another_way.py
1
2
3
[4, 5, 6, 7, 8, 9]
10In 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
Section titled “Unpacking the Nested Tuple”We can also unpack the nested tuple.
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:
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
Section titled “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.
Check yourself
Section titled “Check yourself”-
`a, b = (1, 2, 3)`. What happens?
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.
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.
-
`a, *rest = (1,)`. What is `rest`?
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.
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.
-
What does `*init, last = (1, 2, 3)` bind?
The star may appear anywhere in the shape — the fixed names take their positions and the star absorbs the middle.
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.
-
Why is unpacking preferable to indexing a returned tuple?
Indexing silently reads the wrong element when a function's shape changes. Unpacking raises `ValueError` right there, with the expected and actual counts.
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.
Try it: Tuple Unpacking Exercises
Section titled “Try it: Tuple Unpacking Exercises”Exercise 1 – Basic Unpacking
Section titled “Exercise 1 – Basic Unpacking”Exercise 2 – Star Unpacking
Section titled “Exercise 2 – Star Unpacking”Exercise 3 – Swap with Tuple Unpacking
Section titled “Exercise 3 – Swap with Tuple Unpacking”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading