Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - PACKING UNPACKING TUPLE


Packing and UnPacking a Tuple?


Let us say, we have a Tuple that contains three names, 'Mohan', 'Kriti' and 'Salim'. And when we assign it to a variable, it is called as 'Packing'.


Example :


x = ("Mohan", "Kriti", "Salim")
print(x) 


Output :



  ('Mohan', 'Kriti', 'Salim')

Now, Python provides a way to assign each values to a Tuple to individual variables.


Let us see with the below example.


So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Tuple,


Example :


x = ("Mohan", "Kriti", "Salim")
(i, j, k) = x
print(i)
print(j)
print(k) 


Output :



  Mohan
  Kriti
  Salim

So, we have created a 'Tuple' with three elements, 'Mohan', 'Kriti' and 'Salim'. And assigned them to a variable 'x'.


x = ("Mohan", "Kriti", "Salim")

java_Collections

In the next line, we have 'UnPacked' the 'Tuple' elements to variables, 'i', 'j' and 'k'.


(i, j, k) = x

And the first element of the 'Tuple' (i.e. 'Mohan') is assigned to the variable 'i'.


java_Collections

The second element of the 'Tuple' (i.e. 'Kriti') is assigned to the variable 'j'.


java_Collections

Finally, the third element of the 'Tuple' (i.e. 'Salim') is assigned to the variable 'k'.


java_Collections

And the print statement,


print(i)
print(j)
print(k)

Prints the values,


Mohan
Kriti
Salim

Note : Just remember to count of variable equal to the number of elements in the tuple. i.e. in the above example, we have created three variables for unpacking, 'i', 'j' and 'k'.

But what if we the number of variables are not equal to the number of elements in the 'Tuple'?


In that case you can use an Asterisk '*' any one variable.


Use of asterisk '*' for UnPacking


Let us see with the below example.


Example :


x = ("Mohan", "Kriti", "Salim")
(i, *j) = x
print(i)
print(j)


Output :



  Mohan
  ['Kriti', 'Salim']

And as usual, we have created a 'Tuple' with three elements, 'Mohan', 'Kriti' and 'Salim'.And assigned them to a variable 'x'.


x = ("Mohan", "Kriti", "Salim")

java_Collections

In the next line, we have 'UnPacked' the 'Tuple' elements to variables, 'i' and 'j'.


But the number of elements in the 'Tuple' are three. Which is a mismatch.


And what we do is put an asterisk '*' at in front of the second variable 'j'.


(i, *j) = x

And what happens is,


The first element of the 'Tuple' (i.e. 'Mohan') is assigned to the variable 'i'.


java_Collections

And the second an third element of the 'Tuple' (i.e. 'Kriti' and 'Salim') is assigned to the variable 'j', forming a List.


java_Collections

And the print statement,


print(i)

Prints the String 'Mohan',


Mohan

And since the variable 'j' is a List. So, the print statement,


print(i)

Prints the List,


['Kriti', 'Salim']

Let us see another example with asterisk '*'.


Example :


x = ("Mohan", "Kriti", "Salim")
(*i, j) = x
print(i)
print(j)


Output :



  ['Mohan', 'Kriti']
  Salim

And in the above program, the List is formed with 'Mohan' and 'Kriti' and is assigned to the variable 'i'.