Skip to content

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.

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

    tuple_constructor.py
    data = tuple(('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
    print(data)
    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')
    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)
    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')
    command
    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.

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)
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
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 datadata. We then unpack the tuple and assign the values of the tuple to the variables aa, bb, cc, dd, ee, ff, gg, hh, ii, jj. 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.

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)
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']
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 datadata. We then unpack the tuple and assign the values of the tuple to the variables aa, bb, cc, dd. 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 dd.

Another Way

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)
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
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 datadata. We then unpack the tuple and assign the values of the tuple to the variables aa, bb, cc, dd, ee. 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 dd. The last value of the tuple is assigned to the variable ee.

Unpacking the Nested Tuple

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)
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')
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 datadata. We then unpack the tuple and assign the values of the tuple to the variables aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk. 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 kk.

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