The One About The Taxi Driver, Mappings, and Sequences • A Short Trip to 42 Python Street
How can London cabbies help us learn about mappings and sequences in Python?
It's a crisp early Spring morning in London. Despite its reputation, it doesn't rain that often here. The sun is thinking about rising further in the sky. You step out on the main road and hail a cab.
"42 Python Street, please"
The cabbie's slow nod is the signal to get into the taxi, fasten your seat belt, and wonder whether this will be a chatty taxi driver or a quiet one…
In this short article, we'll wind and weave through the metaphorical streets leading to Data Structure Square. We'll look at mappings and sequences from a different viewpoint.
But first… how much do you know about London cabbies?
London Cabbies • Did You Know?
I've lived in London for a large part of my life. London is a large city—you probably know that already. It's also one that grew organically over two millennia. This means that when you look at it from above, you'll see thousands of squiggly lines with no discernable order.
No wonder the test London taxi drivers–cabbies–need to take is hard. It takes several years to prepare for and pass The Knowledge, which is how taxi drivers refer to the test. London cabbies are renowned for their ability to get anywhere in the city. A research study found that their posterior hippocampus is larger than for subjects in a control group. This part of the brain is used heavily in memory and spatial navigation. And the longer they'd been active as taxi drivers, the larger their posterior hippocampus.
So, this is interesting, but what's its relevance with Python's mappings and sequences?
"42 Python Street, please"
Let's get back to the point when you told the cabbie where you wanted to go and then got into the cab. The taxi driver's enlarged hippocampus, and some other parts of his brain, I guess, kicked into action. The cabbie knew the best route to get you to 42 Python Street.
By the way, there's no real "Python Street" in London, but I didn't want all of you to turn up to someone's home on your next visit to London!
The cabbie doesn't need to go through every street in London to look for Python Street. He just knows where that street is in relation to the starting point and heads directly there. This is the mapping part of the story, but we'll get back to that shortly.
Back to the taxi trip. The cab driver just got to the bottom of Python Street. The Knowledge got him to this point. Now, he can start counting the houses until he finds the 42nd one. Yes, yes, there are numbers on the doors, and he doesn't need to count, but just play along. Also, numbers go up in steps of two since there are odd numbers on one side of the road and even numbers on the other. But why let facts get in the way of a good analogy?! This is the sequence part of this journey.
Mapping London
Let's reverse to the start of the journey. Figuratively not literally! When the cabbie studied to pass The Knowledge, he was creating a mapping between the name of the street and its geographical location. That's what the map of London is—it's a link between a place name and a geographical location.
That's where Python mappings get their name, after all.
Let's look at a dictionary, which is the most common Python mapping:
populations_in_millions = {
"London": 9,
"New York City": 8.4,
"Tokyo": 37,
"Shanghai": 24,
}
In a dictionary, the key takes you directly to its value. It doesn't need to go through each value in the dictionary. The key tells the dictionary where the value is. It achieves this by generating the hash number for the key. The object used as a key can be converted into a unique integer. That's the hash number. The same key will always give the same hash number.
I won't go down the Hash Alley in this article. I'll leave that for a future article. All we need to know is that each key leads to its value efficiently, without having to sift through other values in the dictionary.
This is what happens to the cabbie when you say "42 Python Street, please". The taxi driver will ignore the '42', and probably the 'please', too. "Python Street" is the key linked to the geographical location.
A Sequence of Houses
Once the cab reached Python Street, the mapping had done its job. Now, there's a sequence of houses. They're all in a row, numbered in increasing order. To get to house number 42, the cab driver needs to drive past houses 1 to 41. And yes, let's ignore the fact that in a real London road, the houses go up from 2 to 42, in steps of 2, on that side of the road—this is not relevant here, so we'll pretend it's not the case!
We should probably opt for a tuple to represent this sequence as it's immutable. We could build new houses or demolish existing ones on Python Street, but in general, we can assume the sequence of houses on Python Street is immutable.
Fetching Items From Mappings and Sequences
Before finishing our journey, let's look at how we fetch items from mappings and sequences. Let's use the dictionary we saw earlier and a tuple:
populations_dict = {
"London": 9,
"New York City": 8.4,
"Tokyo": 37,
"Shanghai": 24,
}
populations_tuple = (9, 8.4, 37, 24)
Let's look at the tuple first. Here's a verbose way of describing this assignment:
Associate the value
9
with the integer0
, which is the index indicating the first position in the tupleAssociate the value
8.4
with the integer1
, the index indicating the second positionAssociate the value
37
with the integer2
, the index indicating the third positionAssociate the value
24
with the integer3
, the index indicating the fourth position
This process is implicit when you create the tuple. You don't need to manually create these associations since, in any sequence, the values are assigned based on their order.
Let's shift our attention to the dictionary. Here's what happens when we create this dictionary:
Associate the value
9
with the string"London"
, which is the keyAssociate the value
8.4
with the string"New York City"
Associate the value
37
with the string"Tokyo"
Associate the value
24
with the string"Shanghai"
In this case, you do have to manually create the associations between the key and the value. These links are not done automatically based on order or any other metric, as they are in sequences.
Let's fetch values from these two data structures, starting with the tuple:
>>> populations_tuple[1]
8.4
We can translate this to "fetch the value associated with the index 1".
Now, let's look at the dictionary:
>>> populations_dict["New York City"]
8.4
We can translate this to "fetch the value associated with the key "New York City
".
A Dictionary of Tuples
Our taxi journey through the busy London streets can be represented by a dictionary of tuples. The London street names are the keys of the dictionary. Each key has a tuple of integers as its value with the house numbers on that street:
london = {
"Python Street": tuple(range(1, 87)),
"Tiny Mews": tuple(range(1, 7)),
"Long Road": tuple(range(1, 159)),
# ...
}
We're assuming there are 86 houses in the imaginary Python Street, 6 in Tiny Mews, and 158 in Long Road…
The End of The Road
The cab stops in front of 42. You pay for the ride and leave a generous tip. The cab driver kept you entertained during the journey, telling you how he's been learning Python recently and he's currently learning about mappings and sequences. He sees data structures everywhere around him now as he drives across London.
You leave the cab, shut the door, mutter a "thank you", and walk towards 42 Python Street.
Code in this article uses Python 3.11
Stop Stack
Recently published articles on The Stack:
Deconstructing Ideas And Constructing Code • Using the Store-Repeat-Decide-Reuse Concept. Starting to code on a blank page • How do you convert your ideas into code?
There's A Method To The Madness • Defining Methods In Python Classes. Year 3 at Hogwarts School of Codecraft and Algorithmancy • Defining Methods
Finding Your Way To The Right Value • Python's Mappings. Part 3 of the Data Structure Categories Series
Python's functools.partial() Lets You Pre-Fill A Function. An exploration of partial() and partialmethod() in functools
Let The Real Magic Begin • Creating Classes in Python. Year 2 at Hogwarts School of Codecraft and Algorithmancy • Defining Classes • Data Attributes
Most articles will be published in full on the free subscription. However, a lot of effort and time goes into crafting and preparing these articles. If you enjoy the content and find it useful, and if you're in a position to do so, you can become a paid subscriber. In addition to supporting this work, you'll get access to the full archive of articles and some paid-only articles. Thank you!