6 Examples to Unpack Tuple in Python

Hello Geeks! I hope all are doing great. So today, in this article, we will see the concept of python unpack tuple. First, we will see what are unpack tuples, and how is it used. After that, we will see some examples to understand it more clearly. So, let’s get started.

Tuples

So, tuples are the datatype supported by python programming, which stores multiple values in a single variable. One of the significant properties of the tuple is that it is immutable means you cannot change a value in a tuple. However, you can access them easily. Let’s see some code for it.

Example 1: Creating and Accessing elements of tuple

# Creating a tuple
tuple1 = ('a','b','c','d')
print('Printing the whole tuple',tuple1)

# Accessing elements of tuple
print('First element of tuple',tuple1[0])
print('Slicing the tuple from 1st index element to 2nd index element',tuple1[1:3]) #Returns the tuple
print('Accessing last element of tuple',tuple1[-1])

Output:

Printing the whole tuple ('a', 'b', 'c', 'd')
First element of tuple a
Slicing the tuple from 1st index element to 2nd index element ('b', 'c')
Accessing last element of tuple d

Unpacking the Tuples

Now, once you understand tuples and how to create and access them, let’s see what unpacking a tuple means.

Unpacking the tuples refers to a method in which we can assign the tuple values to some tuple variables. Let’s see some examples to understand them more clearly.

Recommended Reading | [Solved] Valueerror: Too Many Values to Unpack (Expected 2)

Example 2: Unpacking the tuples

# Creating a tuple of national heriatge of India
national_heritage = ('Statue of Unity','Taj Mahal','Qutub Minar','Sanchi Stupa')
print("Printing the Tuple",national_heritage)

# Unpacking tuples as per their state
(Gujrat,UP,Delhi,MP) = national_heritage

# Acceessing elements of unpacked tuple from variables
print('National Heriatge in Gujrat:',Gujrat)
print('National Heriatge in Uttarpradesh:',UP)

Output:

Printing the Tuple ('Statue of Unity', 'Taj Mahal', 'Qutub Minar', 'Sanchi Stupa')
National Heriatge in Gujrat: Statue of Unity
National Heriatge in Uttarpradesh: Taj Mahal

Example 3: Unpacking Tuple as arguments

In this case, we use optional arguments (*args) to unpack a list of values into a variable. Let’s see an example.

# Creating a tuple of national heriatge of India
national_heritage = ('Statue of Unity','Taj Mahal','Agra Fort','Pryagraj Fort','Qutub Minar','Sanchi Stupa')

# Unpacking tuples as per their state
# In the following case optional arguments is passed 
# According to which a tuple of values is unpacked in a single variable
(Gujrat,*UP,Delhi,MP) = national_heritage

# Acceessing elements of unpacked tuple from variables
print('National Heriatge in Uttarpradesh:',UP)

Output:

National Heriatge in Uttarpradesh: ['Taj Mahal', 'Agra Fort', 'Pryagraj Fort']

Example 4: Unpacking Tuples as Dictionary

# Creating a tuple of national heriatge of India
national_heritage = ('Statue of Unity','Taj Mahal','Qutub Minar','Sanchi Stupa')

# Unpacking tuples as dictionary object
dictionary = {}
dictionary['Gujrat'],dictionary['UP'],dictionary['Delhi'],dictionary['MP'] = national_heritage
print(dictionary)

Output:

{'Gujrat': 'Statue of Unity', 'UP': 'Taj Mahal', 'Delhi': 'Qutub Minar', 'MP': 'Sanchi Stupa'}

Example 5: Unpacking nested tuples using list comprehension

We can also unpack nested tuples. For that, we have to make use of list comprehension. Let’s see an example to understand it more clearly.

tuple1 = (('x','y','z'),('i','j','k'))
new_tuple = [( p,q,r) for p,q,r in tuple1]
print(new_tuple)

Output:

[('x', 'y', 'z'), ('i', 'j', 'k')]

Example 6: Unpacking tuples using lambda function

tuple1 = (1,2)

my_lambda = lambda (x,y) : x+y
print(my_lambda(tuple1))

Output:

3

[Note: Unpacking using lambda function is available only till python2 and unavailable in python3.]

FAQs on Python Tuple Unpack

Can python unpack tuple have a default value?

There is no inbuilt functionality of unpacking tuples with default values; however, you can do it using the following code:

tuple1 = ('a',)
(x,*y) = (tuple1)
print(x)
print('Value of y',y)

y = y[0] if y else 'Your Value'
print('Value of y',y)

How to Unzip a list of tuples?

You can use the following line of code;

tuple_list = [('x',1),('y',2),('z',3)]

new_list = [[i for i,j in tuple_list], [j for i,j in tuple_list]]
print(new_list)

Conclusion

So, today in this article, we have seen that what does unpacking tuples means and how it can be proved as one of the powerful tools in python programming. We have seen some of the examples to understand unpacking tuples better.

Click here to learn about enumerate function.

I hope this article has helped you. Thank You.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments