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.