You are currently viewing Python List Unpacking with Examples

In Python, Tuple unpacking is a feature that allows us to assign values to multiple variables in a single statement. It works by unpacking a sequence (e.g., a tuple, list, or string) into individual variables. Unpacking is not limited to lists, you can also use it to unpack tuples, strings, and other iterable objects. Unpacking is a powerful feature in Python that can make your code more concise and readable.

Advertisements

You can unpack a list in Python by using many ways, for example, by using * unpacking, nested lists, dictionary, and lambda. In this article, I will explain list unpacking by using all these methods with examples.

1. Quick Examples of Unpacking the List

If you are in a hurry, below are some quick examples of unpacking the list.


# Quick examples of list unpacking

# Example 1: Unpack the list into variables
mylist = [2, 4, 6]
a, b, c = mylist

# Example 2: Use * to unpack list
mylist = [2, 4, 6, 8, 10]
a, b, *c = mylist

# Example 3: Using nested lists
nested_list = ['Sparkby', [1, 2, 3], 'Examples']
a, (b, c, d), e = nested_list

# Example 4: Use * to unpack only a part of the list
mylist = [2, 4, 6, 8, 10]
result = [*mylist[:2]]

# Example 5: Unpacking list as arguments
def technology(courses, fees, duration):
    print(courses)
    print(fees)
    print(duration)  
mylist = ['Python', 25000, '50days']
technology(*mylist)

# Example 6: Unpacking list as a dictionary object
mylist = ['Python', '50days', 25000]
dictionary = {}
dictionary['courses'],dictionary['duration'],dictionary['fees'] = mylist

# Example 7: Unpacking list 
# Using lambda function 
mylist = [4, 7]
result = (lambda x,y: x + y)(*mylist)

2. Using the * Unpacking Method

You can use unpack operator * on a list to extract the individual elements from a list and assign them to variables. For example, you have a list mylist with three elements. Then you can use list unpacking to assign each element of the list to a separate variable a, b, and c.


# Initialize list
mylist = [2, 4, 6]
print("Original list: ", mylist)

# Unpack the list into variables
a, b, c = mylist

print("Unpack the list into variable: ",a)
print("Unpack the list into variable: ", b) 
print("Unpack the list into variable: ", c)

Yields below output.

python list unpacking

3. Unpack Specified Elements in a List

Similarly, you can use the * unpacking operator to assign the first few elements of a list to one variable and the remaining elements to another variable. For instance, you can use list unpacking to assign the first two elements of mylist to variables a and b, and the remaining elements to a list variable c, using the * operator.


# Initialize list 
mylist = [2, 4, 6, 8, 10]

# Use * to unpack list
a, b, *c = mylist

print(a) 
print(b) 
print(c)

# Output:
# 2
# 4
# [6, 8, 10]

4. Unpacking Nested List

List unpacking also works with nested lists. For example, the nested list nested_list is unpacked into five variables a, b, c, d, and e. The first element of the list 'Sparkby', is assigned to a, the second element, [1, 2, 3], is unpacked into the variables b, c, and d, and the third element, 'Examples', is assigned to e.


# Using nested lists
nested_list = ['Sparkby', [1, 2, 3], 'Examples']
a, (b, c, d), e = nested_list
print(a)   
print(b)   
print(c)   
print(d)   
print(e) 

# Output:
# Sparkby
# 1
# 2
# 3
# Examples

5. Use * to Unpack only a Part of the List

Similarly, you can use the * operator to unpack only a part of the list into a new list. For example, you can use the * operator to unpack only the first two values of mylist into a new list called result. You can use a list slice to select the first two elements of mylist and then use the * operator to unpack those elements into a new list.


# Initialize list
mylist = [2, 4, 6, 8, 10]
print("Original list: ", mylist)

# Use * to unpack only a part of the list
result = [*mylist[:2]]
print("Unpack list: ", result)

Yields below output.

python list unpacking

6. Unpacking List as Arguments

You can unpack a list and pass its elements as arguments to a function in Python. This is a convenient way to pass multiple arguments to a function without having to manually specify each argument.

For example, you define a function technology that takes three arguments: courses, fees, and duration. Next, you define a list called mylist with three elements: 'Python‘, 25000, and '50days'. Finally, you call the technology function with the unpacked elements using the * operator. This means that each element of mylist will be passed as a separate argument to the technology function.


# Unpacking list as arguments
def technology(courses, fees, duration):
    print(courses)
    print(fees)
    print(duration)
    
mylist = ['Python', 25000, '50days']
technology(*mylist)

# Output:
# Python
# 25000
# 50days

7. Unpacking List as Dictionary

To unpack a list and create a dictionary object in Python. For instance, you can define a list called mylist with three elements, 'Python', '50days', and 25000. Next, you can define an empty dictionary called dictionary. Then you can use dictionary unpacking to assign the values mylist to the keys of the dictionary. The left-hand side of the assignment specifies the keys of the dictionary, while the right-hand side specifies the values from mylist.


# Unpacking list as dictionary object
mylist = ['Python', '50days', 25000]

dictionary = {}
dictionary['courses'],dictionary['duration'],dictionary['fees'] = mylist
print(dictionary)

# Output:
# {'courses': 'Python', 'duration': '50days', 'fees': 25000}

8. Unpacking List Using Lambda Function

You can also use a lambda function to unpack a list in Python. For example, you define a lambda function that takes two arguments x and y, and returns their sum. Use the * operator to unpack the mylist and pass their contents as arguments to the lambda function. The result is the sum of the two values in the list, which is 11.


# Unpacking list 
# Using lambda function 
mylist = [4, 7]
result = (lambda x,y: x + y)(*mylist)
print(result)

# Output:
# 11

9. Conclusion

In this article, I have explained how to unpack the list by using multiple methods and operators of Python such as (*) unpacking operator, list slice, nested list, dictionary, and lambda with examples.

Happy Learning !!