Sequence Unpacking in Python. Understanding Python Sequence Unpacking | by Sadrach Pierre, Ph.D. | Towards Data Science

Sequence unpacking in python allows you to take objects in a collection and store them in variables for later use. This is particularly useful when a function or method returns a sequence of objects. In this post, we will discuss sequence unpacking in python.
Let’s get started!
A key feature of python is that any sequence can be unpacked into variables by assignment. Consider a list of values corresponding to the attributes of a specific run on the Nike run application. This list will contain the date, pace (minutes), time (minutes), distance (miles) and elevation (feet) for a run:
new_run = ['09/01/2020', '10:00', 60, 6, 100]
We can unpack this list with appropriately named variables by assignment:
date, pace, time, distance, elevation = new_run
We can then print the values for these variables to validate that the assignments were correct:
print("Date: ", date)print("Pace: ", pace, 'min')print("Time: ", time, 'min')print("Distance: ", distance, 'miles')print("Elevation: ", elevation, 'feet')
The elements in the sequence we are unpacking can be sequences as well. For example, in addition to the average pace of the overall run, we can have a tuple of mile splits:
new_run_with_splits = ['09/01/2020', '10:00', 60, 6, 100, ('12:00', '12:00', '10:00', '11;00', '8:00', '7:00')]
Let’s now unpack our new sequence:
date, pace, time, distance, elevation, splits = new_run_with_splits
And we can print the mile splits:
print("Mile splits: ", splits)
We can even further unpack the splits list in the previous code. Let’s unpack the split lists with variables denoting the mile number:
date, pace, time, distance, elevation, (mile_2, mile_2, mile3_, mile_4, mile_5, mile_6) = new_run_with_splits
Let’s print the mile variables:
print("Mile Two: ", mile_2)print("Mile Three: ", mile_3)print("Mile Four: ", mile_4)print("Mile Five: ", mile_5)print("Mile Six: ", mile_6)
We can also use the ‘_’ character to leave out unwanted values. For example if we want to leave out date and elevation we do:
_, pace, time, distance, _, (mile_2, mile_2, mile_3, mile_4, mile_5, mile_6) = new_run_with_splits
We can also unpack an arbitrary number of elements using the python “star expression” (*). For example, if we want to store the first and last variables and store the middle values in a list we can do the following:
date, pace, time, distance, elevation, (first, *middle, last) = new_run_with_splits
Let’s print the first, middle and last variables:
print("First: ", first)print("Middlle: ", middle)print("Last: ", last
I’d like to emphasize that the objects in our sequence can be of any type. For example, we could have used a dictionary instead of a tuple for the mile splits:
new_run_with_splits_dict = ['09/01/2020', '10:00', 60, 6, 100, {'mile_1': '12:00', 'mile_2':'12:00', 'mile3':'10:00', 'mile_4':'11;00', 'mile_5':'8:00', 'mile_6':'7:00'}]
Let’s unpack our new list:
date, pace, time, distance, elevation, splits_dict = new_run_with_splits_dict
Now we can access the mile split values by key:
print("Mile One: ", splits_dict['mile_1'])print("Mile Two: ", splits_dict['mile_2'])print("Mile Three: ", splits_dict['mile_3'])
I’ll stop here but I encourage you to play around with the code yourself.
In this post, we discussed how to unpack sequences in python. First, we showed how to unpack a list of values associated with a run published to the Nike Run application. We also showed that the values in the sequences can also be sequences, where the sequence or its elements can be stored in separate variables for later use. We then showed how we can leave values out using the underscore character. Next, we discussed how to unpack an arbitrary number of objects using the python “star expression”. Finally, we demonstrated how we can unpack a dictionary object from a list of objects and access the dictionary values by key. I hope you found this post interesting/useful. The code in this post is available on GitHub. Thank you for reading!