EN
Python - unpack collection
0 points
In this article, we would like to show you how to unpack collection in Python.
In this example, we use universal method to unpack a list but you can use it to extract the values back into variables from any collection.
xxxxxxxxxx
1
my_list = [1, 2, 3]
2
3
x, y, z = my_list
4
5
print(x)
6
print(y)
7
print(z)
Output:
xxxxxxxxxx
1
1
2
2
3
3
In this example, we use the same method to unpack a tuple.
xxxxxxxxxx
1
my_tuple = (1, 2, 3)
2
3
x, y, z = my_tuple
4
5
print(x)
6
print(y)
7
print(z)
Output:
xxxxxxxxxx
1
1
2
2
3
3