- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unpacking a Tuple in Python
Python offers immutable data types known as tuples. In this article, we will learn about packing an unpacking tuple type in Python 3.x. Or earlier.
Packing and Unpacking a Tuple
Python offers a very powerful tuple assignment tool that maps right hand side arguments into left hand side arguments. THis act of mapping together is known as unpacking of a tuple of values into a norml variable. WHereas in packing, we put values into a regular tuple by means of regular assignment.
Now let’s take a look at its implementation −
# Packing tuple varibles under one varible name tup = ("Tutorialspoint", "Python", "Unpacking a tuple") # Packing tuple varibles into a group of arguments (website, language, topic) = tup # print college name print(website,"\t",language," ",topic)
Tutorialspoint Python Unpacking a tuple
During the unpacking of tuple, the total number of variables on the left-hand side should be equivalent to the total number of values in given tuple tup.
Python gives the syntax to pass optional arguments (*arguments) for tuple unpacking of arbitrary length. All values will be assigned to every variable in the order of their specification and all remaining values will be assigned to *arguments . Let’s consider the following code.
# Packing tuple variables under one variable name tup = ("Tutorialspoint", "Python","3.x.",":Data Structure","Unpacking a tuple") # Packing tuple variables into a group of arguments (website,*language, topic) = tup # print college name print(website,"\t",*language," ",topic)
Tutorialspoint Python 3.x. :Data Structure Unpacking a tuple
In python tuples can be unpacked using a function in function tuple is passed and in function, values are unpacked into a normal variable. The following code explains how to deal with an arbitrary number of arguments.
“*_” is used to specify the arbitrary number of arguments in the tuple.
# Packing tuple varibles under one varible name tup = ("Tutorialspoint", "Python","3.x.","Data Structure:","Unpacking a tuple") # UnPacking tuple variables into a group of arguments and skipping unwanted arguments (website,*_,typ,topic) = tup # print college name print(website,"\t",typ," ",topic)
Tutorialspoint Data Structure: Unpacking a tuple
In case we want to skip only one argument then we can replace “*_” by “_”
In this article, we learnt how we can pack and unpack tuples in a variety of Ways.
- What is tuple unpacking in Python?
- Unpacking tuple of lists in Python
- Packing and Unpacking Arguments in Python?
- How we can use Python Tuple within a Tuple?
- How can I append a tuple into another tuple in Python?
- Flatten tuple of List to tuple in Python
- Convert a list into a tuple in Python.
- Tuple Division in Python
- Tuple multiplication in Python
- Python - Join tuple elements in a list
- How can I subtract tuple of tuples from a tuple in Python?
- How to iterate through a tuple in Python?
- Tuple Data Type in Python
- Delete Tuple Elements in Python
- Tuple XOR operation in Python