Unpack the Tuple
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.
-
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.pydata = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j') print(data)
tuple.pydata = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j') print(data)
Output:
commandC:\Users\username>python tuple.py ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
commandC:\Users\username>python tuple.py ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
-
Using the
tuple()
tuple()
constructor. You can also create a tuple using thetuple()
tuple()
constructor. The following example shows how to create a tuple using thetuple()
tuple()
constructor.tuple_constructor.pydata = tuple(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) print(data)
tuple_constructor.pydata = tuple(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) print(data)
Output:
commandC:\Users\username>python tuple_constructor.py ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
commandC:\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.pydata = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' print(data)
tuple_comma.pydata = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' print(data)
Output:
commandC:\Users\username>python tuple_comma.py ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
commandC:\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.
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)
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
j
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
data
. We then unpack the tuple and assign the values of the tuple to the variables a
a
, b
b
, c
c
, d
d
, e
e
, f
f
, g
g
, h
h
, i
i
, j
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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
a, b, c, *d = data
print(a)
print(b)
print(c)
print(d)
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']
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
data
. We then unpack the tuple and assign the values of the tuple to the variables a
a
, b
b
, c
c
, d
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
d
.
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)
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]
10
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
data
. We then unpack the tuple and assign the values of the tuple to the variables a
a
, b
b
, c
c
, d
d
, e
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
d
. The last value of the tuple is assigned to the variable e
e
.
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)
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')
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
data
. We then unpack the tuple and assign the values of the tuple to the variables a
a
, b
b
, c
c
, d
d
, e
e
, f
f
, g
g
, h
h
, i
i
, j
j
, k
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
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.
Was this page helpful?
Let us know how we did