capypad
0 day streak
python / beginner
Snippet

Unpacking Values from a Tuple

Tuple unpacking allows you to assign each item in a tuple (or list) to a separate variable in one single line of code.

snippet.py
python
1
2
3
4
coordinates = (5, 12)
x, y = coordinates
print(x)
print(y)
Breakdown
1
coordinates = (5, 12)
Creates a tuple containing two integers.
2
x, y = coordinates
Assigns the first value to x and the second value to y simultaneously.