Python Program to Unpack Tuple Items

Write a Python Program to Unpack Tuple Items. By assigning each tuple item to a variable, we can unpack the tuple items. Here, we assigned four tuple items to four different variables.

# Unpack Tuple Items

emp = ("John", 22, "Developer", 45000)
print(emp)

name, age, profession, sal = emp
print(name)
print(age)
print(profession)
print(sal)
Python Program to Unpack Tuple Items 1

By using the *, we can assign multiple unpacked tuple items to a single variable. For instance, name, *age, sal = emp unpack emp tuple and assign name as John, sal as 45000, and all the remaining tuple items to age.

# Unpack Tuple Items

emp = ("John", "Root", 22, "Developer", 45000)
print(emp)

name, *age, sal = emp
print(name)
print(age)
print(sal)

print("==========")
name, age, *sal = emp
print(name)
print(age)
print(sal)

print("==========")
*name, age, prof, sal = emp
print(name)
print(age)
print(prof)
print(sal)

Python tuple unpack output

('John', 'Root', 22, 'Developer', 45000)
John
['Root', 22, 'Developer']
45000
==========
John
Root
[22, 'Developer', 45000]
==========
['John', 'Root']
22
Developer
45000