Basic Unpacking
1coords = (40.7128, -74.0060)
2lat, lon = coords
3print(lat) # 40.7128
4print(lon) # -74.0060
Tuple unpacking is a clean way to extract values from a tuple and assign them to individual variables. Perfect for coordinate pairs, function returns, and swapping values.
Left side names line up with tuple items.
Unpacking takes the items from a tuple and gives each one its own variable. Instead of accessing with tuple[0], tuple[1], you can write x, y = tuple and use x and y directly.
Used when extracting coordinates, getting function results with multiple values, swapping variables, iterating over lists of tuples in loops, and database queries with multiple columns.
Instead of coords[0] and coords[1], write lat, lon = coords. Now you have clear variable names. Same idea for RGB colors: red, green, blue = (255, 128, 0).