Skip to content

Python unpack list

  • by

In Python, unpacking a list refers to the process of extracting individual elements from a list and assigning them to separate variables. This can be done using the unpacking operator (*) or by directly assigning the elements to variables.

The syntax for unpacking lists in Python involves using the unpacking operator (*) and variable names to assign elements from the list to those variables. Here’s the basic syntax:

# Unpacking with the * operator
variable1, variable2, *rest_of_elements = list_to_unpack
  • variable1, variable2, and rest_of_elements are the variable names you want to assign the unpacked elements to.
  • list_to_unpack is the list that you want to unpack.

The *rest_of_elements part is used to capture the remaining elements in the list that couldn’t be assigned to the earlier variables. It collects these elements into a list (or another iterable) called rest_of_elements.

Here are a few examples to illustrate the syntax:

# Basic unpacking
numbers = [10, 20, 30]
x, y, z = numbers

# Unpacking with a variable to collect remaining elements
my_list = [1, 2, 3, 4, 5]
first, *middle, last = my_list

# Unpacking nested lists
nested_list = [1, [2, 3], 4, [5, 6, 7]]
a, b, c, d = nested_list

Remember that the number of variables on the left-hand side of the unpacking assignment should match the number of elements in the list you’re unpacking. If they don’t match, you’ll get a ValueError.

Also, note that you can use _ as a variable name to indicate that you’re not interested in the value of a particular element being unpacked. For example:

my_list = [100, 200, 300, 400]
x, _, *rest = my_list

In this case, the second element (200) will be ignored, and the rest of the elements (300, 400) will be collected into the rest variable.

Python unpack list example

Simple example code.

1. Unpacking with the * operator:

my_list = [1, 2, 3, 4, 5]
first, *middle, last = my_list

print("First:", first)
print("Middle:", middle)
print("Last:", last)

2. Unpacking without the * operator:

my_list = [10, 20, 30]
x, y, z = my_list

print("x:", x)
print("y:", y)
print("z:", z)

3. Unpacking with a loop:

coordinates = [(1, 2), (3, 4), (5, 6)]

for x, y in coordinates:
    print("x:", x, "y:", y)

4. Unpacking when the list length and variable count don’t match:

numbers = [100, 200, 300, 400]
x, y, *rest = numbers

print("x:", x)
print("y:", y)
print("Rest:", rest)

5. Unpacking nested lists:

nested_list = [1, [2, 3], 4, [5, 6, 7]]
a, b, c, d = nested_list

print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)


Python unpack list

Unpacking is a powerful feature in Python that allows you to easily extract and work with elements from lists or other iterable data structures.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading