Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Tuple Unpacking in Python


Tuple unpacking is a powerful feature in Python that allows you to assign multiple variables at once from a tuple or an iterable. This feature simplifies working with sequences of values and enhances code readability.

1. What is Tuple Unpacking?

Tuple unpacking in Python refers to the process of assigning the elements of a tuple to individual variables. This allows you to access each element of the tuple directly and in a clean way.

Basic Example:

    # Tuple unpacking example
    person = ("John", 25, "Engineer")
    name, age, profession = person
    print(name)       # Outputs: John
    print(age)        # Outputs: 25
    print(profession) # Outputs: Engineer
        

2. Unpacking with Different Lengths

Sometimes, you may want to unpack only part of a tuple, or you may have tuples of varying lengths. Python provides a way to handle this using an asterisk (*) for collecting multiple values into a list.

Example: Unpacking with Asterisk (*)

    # Unpacking with asterisk (*)
    person = ("John", "Doe", 25, "Engineer")
    first_name, *other_info = person
    print(first_name)      # Outputs: John
    print(other_info)      # Outputs: ['Doe', 25, 'Engineer']
        

Example: Unpacking with Multiple Asterisk (*)

    # Unpacking with multiple asterisks
    person = ("John", "Doe", 25, "Engineer", "USA")
    first_name, *middle_info, country = person
    print(first_name)      # Outputs: John
    print(middle_info)     # Outputs: ['Doe', 25, 'Engineer']
    print(country)         # Outputs: USA
        

3. Swapping Values Using Tuple Unpacking

Tuple unpacking can be used for swapping values between two variables without needing a temporary variable.

Example: Swapping Variables

    # Swapping values
    a = 5
    b = 10
    a, b = b, a
    print(a)  # Outputs: 10
    print(b)  # Outputs: 5
        

4. Unpacking Nested Tuples

You can also unpack nested tuples, where each element itself is a tuple.

Example: Unpacking Nested Tuples

    # Unpacking a nested tuple
    nested = (("John", "Doe"), 25, "Engineer")
    (name, surname), age, profession = nested
    print(name)       # Outputs: John
    print(surname)    # Outputs: Doe
    print(age)        # Outputs: 25
    print(profession) # Outputs: Engineer
        

5. Using Tuple Unpacking in Loops

Tuple unpacking can be particularly useful when iterating over lists of tuples. This allows you to extract elements into individual variables directly in the loop.

Example: Looping with Tuple Unpacking

    # Looping over a list of tuples
    people = [("John", 25), ("Jane", 30), ("Doe", 35)]
    for name, age in people:
        print(name, age)
        # Outputs:
        # John 25
        # Jane 30
        # Doe 35
        

6. Tuple Unpacking with Functions

Tuple unpacking can also be useful when working with functions that return multiple values. Instead of accessing each element individually, you can unpack the returned tuple directly into variables.

Example: Function Returning a Tuple

    # Function returning multiple values
    def get_person_info():
        return ("John", 25, "Engineer")

    name, age, profession = get_person_info()
    print(name)       # Outputs: John
    print(age)        # Outputs: 25
    print(profession) # Outputs: Engineer
        

7. Conclusion

Tuple unpacking in Python is a convenient and readable feature that allows you to assign multiple values in one line, swap values, and handle complex data structures efficiently. It’s a powerful tool for anyone working with sequences of data.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java