capypad
0 day streak
python / beginner
Snippet

Tuple Unpacking

Unpacking allows you to assign the individual elements of a tuple (or list) to multiple variables in a single line of code.

snippet.py
python
1
2
3
4
5
coordinates = (10, 20)
x, y = coordinates
 
print(x)
print(y)
Breakdown
1
x, y = coordinates
Assigns 10 to x and 20 to y by 'unpacking' the tuple.
2
print(x)
Outputs the value stored in variable x.