Pages

Saturday, March 20, 2021

Tuple Unpacking in Python

Tuple Unpacking

Tuple is a collections of data (of different data types) and are created by specifying the data (or variables) inside round brackets. We can also call this as Tuple packing. See more about working with tuples.

So, what is tuple unpacking? As the name suggests unpacking the data from a tuple into variables is called tuple unpacking.

Let's have a quick look at the creation of a tuple (packing) and unpacking of a tuple. 

Tuple unpacking in Python

In the above example, 
  • Line - 2: Creating variable num_one to store a number (1).
  • Line - 4: Creating variable char_one to store a string (ONE). 
  • Line - 8: Creating a tuple by mentioning the variables created inside round brackets (num_one, char_one). This can also be done by directly providing the data inside round brackets (1, "ONE").
  • Line - 11: Unpacking the tuple, unpacking the data in a tuple into different variables. First element in the tuple would be assigned to the first variable and second element in the tuple would be assigned to the second variable. 
  • Lines - 13 & 14: Printing the data from the unpacked variables.

We have not used round brackets to do the tuple unpacking. But, this should work in the same way even if we specify the variables inside round brackets for unpacking the data from a tuple. 

Tuple unpacking in Python

In the above example, we are unpacking a tuple with two elements to two different variables. Similarly, we can do the same for any number of equal variables. 

What to do if there are more number of elements in the tuple than the variables? 

Tuple unpacking would fail with "ValueError: too many values to unpack". 

Tuple unpacking in Python

Tuple Unpacking  - Value Error

So, how to deal with these kind of issues? Well, there isn't straight forward answer to this and depends on why we are moving more number of elements (from tuple) to a less number of variables. 

If this is intended and group of tuple elements should be assigned to a single variable (as list), this can be done by using asterisk (*) before the variable. 

Let's take a look at different example to understand this better. 

Tuple Unpacking in Python

In the above example, 
  • Tuple has three elements.
    • First element is the color. 
    • Second and Third elements are the fruits in the specific color. 
So the requirement here is to create a variable called 'color' to hold the color from tuple and create a list called 'fruits' to hold the list of fruits from tuple. 

If we use the variable names color and fruits as it is, ValueError would be thrown. So, we are using asterisk (*) before 'fruits' which needs to store the fruits as a list. 

Result here would be, 
  • Mapping of first variable to the first element (no asterisk for color, so would consider as one to one mapping). 
  • Mapping of second variable (list) to the remaining elements in the tuple. 
There are few points to consider here.
  • An empty list would be created if there is no corresponding data present in the tuple.     
Tuple unpacking in Python

  • A list would be created with just one element if there is only one corresponding entry in the tuple. Unlike the the variable with the type of data (if asterisk is not used).
Tuple unpacking in Python

  • A list would be created with multiple elements if there are multiple entries in the tuple. 
Tuple unpacking in Python

One thing to note here is a list variable doesn't necessarily be the second or last, this can be in any sequence. 

Let's take if we use asterisk before the color in the above example.

Tuple Unpacking in Python

color would be created as a list with the first two (or more if present) elements and fruits (str) would be created with the last element. 

We can only have one starred expression in an assignment.

Let's take another example with three variables. 

Tuple unpacking in Python

In this example, 
  • We are using three variables and given asterisk for the middle variable. 
  • First and Last elements in the tuple are assigned to the first and last variables. 
  • All other elements in the middle (from Tuple) are assigned as a list to the starred variable. 

Tuple unpacking would be helpful in many scenarios. We will have a look at how we can use tuple unpacking with dictionaries to unpack key and value from dictionary. 

Dictionary method items would return each key and value combination as a tuple. 

Tuple unpacking with dictionaries in Python

In the above example, for loop is repeated for every item (key and value) of a dictionary and the same would be returned as a tuple. 

Let's use tuple unpacking to unpack key and values.

Tuple unpacking with dictionaries in Python

Key and value are unpacked from tuple and can be used separately inside for loop. 

Hope the above info was useful in understanding Tuple unpacking in Python. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

No comments:

Post a Comment

Exception Handling in Python (try - except-else-finally)

Exception Handling in Python Exception handling is key for any programming language. When an exception occurs Python interpreter stops execu...