In this tutorial, we’ll learn what Python tuple unpacking is and how to properly use it. We’ll be using proper examples with step by step explanation in order to better understand its role and benefits.
Outline
- Introduction: Python Tuple Unpacking
- Syntax of Tuple Unpacking In Python
- Implementing Python Tuple Unpacking (Easy Examples)
- Method 1: Using Index Method
- Method 2: Assign Variables Manually
- Method 3: Using Python Tuple Unpacking Method
- Important Program (Tuple, List and Dictionary)
- Note
- Conclusion
Introduction: Python Tuple Unpacking
Python tuple is used to store a list of items and its mutable, which means we cannot change items of a tuple once they are assigned. Click here if you want to learn about Python tuples in detail.
So when we create a tuple and assign items to it, its called Python tuple packing but when we want to assign each item of an existing tuple to a variable, then its called Python tuple unpacking.
Let’s now understand it with proper Python code examples.
Syntax of Tuple Unpacking In Python
a , b , c = ( item1, item2, item3 )
We can also create a Python tuple, assign it and use it to unpack. See below:
tupleValues = (item1, item2, item3, item4) a, b, c, d = tupleValues
- First item will be assigned to the first variable. Second item of tuple to the second variable and so on.
- Each variable must be separated by comma(,).
- The number of variables should match the number of items in Python tuple. If not, then a value error will occur.
Implementing Python Tuple Unpacking (Easy Examples)
Let’s first create a simple Python tuple and assign some items to it. See below:
tupleItems = ( 234, 564, 9878 )
This tuple has 3 integer items. Let’s now try to add all of them.
Method 1: Using Index Method
result= tupleItems[0] + tupleItems[1] + tupleItems[2] print(result)
Output
10676
Items are added and the sum is stored but the code will look very bulky if the length of items are 10, 20 or even more.
Method 2: Assign Variables Manually
a=tupleItems[0] b=tupleItems[1] c=tupleItems[2] result= a+b+c print(result)
Output
10676
We’ve assigned each item to a unique variable but still this method will increase bulkiness of code if the number of items are greater.
Method 3: Using Python Tuple Unpacking Method
a, b, c = tupleItems result= a + b + c print(result)
Output
10676
As we can see that by using tuple unpacking, we can easily assign each item of tuple to a different variable and avoid bulkiness of code.
Important Program (Tuple, List and Dictionary)
- In this program, we’ve created a Python tuple and assign some items to it.
- We also have created an empty Python list and an empty Python dictionary.
- After that, by using for loop we’ve added some items to the list and the length of these items matches with the length of existing Python tuple. In order to prove it, we’ve displayed the values as well.
- Again we’ve used Python for loop to add key value pairs in Python dictionary. Items of Python list will be stored as keys will the items of tuple will be stored as values. Finally, we’ve displayed it. See below code:
valsTuples=[1,2,3,4,5,6,7,8,9,10,'Zeeshan',[4,5],34.7] listItems=[] dictValues={} for val in range(len(valsTuples)): listItems.append(f'value{val}') print(listItems) for val in range(len(listItems)): dictValues[listItems[val]]=valsTuples[val] print(dictValues)
Output
['value0', 'value1', 'value2', 'value3', 'value4', 'value5', 'value6', 'value7', 'value8', 'value9', 'value10', 'value11', 'value12'] {'value0': 1, 'value1': 2, 'value2': 3, 'value3': 4, 'value4': 5, 'value5': 6, 'value6': 7, 'value7': 8, 'value8': 9, 'value9': 10, 'value10': 'Zeeshan', 'value11': [4, 5], 'value12': 34.7}
Hope this program will help you in your future Python projects.
Note
We can also unpack Python list using the same process. We want you to give it a try and share your experience with us in the comment section.
We hope you now have a complete idea of how Python tuple unpacking works and how to proper make use of it. Do share it with your squad of Python developers as well.
Feel free to ask if you still have any questions related to Python tuple unpacking. We’ll be very glad to answer all your questions.
Conclusion
As a conclusion of this article, hope you now have an in-depth practical knowledge of how to properly use Python tuple unpacking. We’d love to receive your valuable feedback on this article.
Do visit our other posts on Python programming. Thank you for reading this article.