Signup/Sign In

Tuple unpacking in Python

Posted in Programming   LAST UPDATED: NOVEMBER 3, 2019

    We know what a tuple is and how it can be used in Python programs. In this post, we will understand the concept of tuple unpacking.

    Tuple unpacking refers to breaking down the tuple with each data element of the tuple assigned to a variable. For example, if your tuple has 4 values stored in it, then you can provide a tuple of 4 variables in it, and use the tuple unpacking technique to unpack the tuple values and store them into the variables provided.

    Whereas, Tuple packing basically means assigning multiple values into a single variable. In this case, we are packing multiple values into a single variable, which is a tuple.




    Tuple packing and unpacking

    Tuple packing is nothing but the creation of tuple, whereas tuple unpacking means to extract tuple values and store them in individual variables.

    tuple_packing = ("StudyTonight", 'Technical website', 12)
    # below line unpacks the data into separate variables
    (name_of_website, type_of_website, no_of_characters) = tuple_packing
    print(name_of_website)
    print(type_of_website)
    print(no_of_characters)

    Output:

    StudyTonight
    Technical website
    12

    When a tuple is being unpacked, there are certain rules which must be adhered to. When a tuple is being packed, a certain number of values are provided to a single variable. The same number of values must be present during unpacking. If the number of values passed is either less or more, it results in a ValueError.

    When the number of values passed during unpacking is more:

    tuple_packing = ("StudyTonight", 'Technical website', 12)   
    # below line unpacks the data into separate variables
    (name_of_website, type_of_website, no_of_characters, location) = tuple_packing

    Output:

    ValueError: not enough values to unpack (expected 4, got 3)

    When the number of values passed during unpacking is less:

    tuple_packing = ("StudyTonight", 'Technical website', 12)   
    # below line unpacks the data into separate variables
    (name_of_website, type_of_website) = tuple_packing

    Output:

    ValueError: too many values to unpack (expected 2)



    Passing a tuple as a parameter to a method:

    Below is an example that demonstrates how tuple packing can be done and the variable assigned to it can be passed to a method. When this method is called, it unpacks the values present within the variable and uses them to perform required operations.

    def calculate_power(x, y): 
        return pow(x, y) 
    
    print("Passing variables as is")
    print(calculate_power(10, 2)) # Calling method by passing normal variables
    
    tuple_packing = (10, 2)
    print("Passing packed values")   
    # Calling method by packing the variables into a tuple 
    # and calling it using the * operator
    print (calculate_power(*tuple_packing))

    Output:

    Passing variables as is
    100
    
    Passing packed values
    100

    Note: The * operator is the unpacking operator in python, and it works with a tuple, list, etc data structures to unpack values stored in them. In the example above, we have a function calculate_power(x, y) which takes in two arguments, which we can provide using normal arguments or we can pass a tuple with 2 values, along with the unpacking operator *. If the tuple has more than 2 values in it, then we will get an error because the function expects only 2 values.


    Tuple unpacking when the number of variables is varying:

    We can use the * operator to assign multiple values to a single variable during tuple unpacking if the number of variables provided is less than the number of data elements present in the tuple. In the example below, we have used the * operator with n, m and once with s, to hold extra data as a list.

    m, *n, s = (12, "study ", "Tonight", 'Technical') 
    print(m) 
    print(n) 
    print(s) 
    print("\n")  
    *m, n, s = (12, "study ", "Tonight", 'Technical') 
    print(m) 
    print(n) 
    print(s)
    print("\n")  
    m, n, *s = (12, "study ", "Tonight", 'Technical') 
    print(m) 
    print(n) 
    print(s)

    Output:

    12
    ['study ', 'Tonight']
    Technical
    
    [12, 'study ']
    Tonight
    Technical
    
    12
    study 
    ['Tonight', 'Technical']



    Conclusion

    In this post, we understood how tuples can be packed and unpacked for various purposes. Don't forget to run the code on your IDE with different data and check how it works.

    Published by: SmritiSatyan
    Tags:PythonPython TupleTuple Unpacking
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS