Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Dictionaries
Lecture: Why dictionaries
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now we are going to focus on a really important set of topics revolving around dictionaries in Python. So, the first question you might ask is like:
0:09
"Why should we focus on dictionaries in Pythonic code?" Well, it turns out that dictionaries are everywhere in Python,
0:16
you'll see that dictionaries are the backing store for many types, so for example when you create a new object from a custom class you've created,
0:23
every instance has its own backing store which is a dictionary for the fields and whatnot that you add to this class.
0:31
Dictionaries are isomorphic with JSON, so you'll see that there is basically a one to one mapping between Python dictionaries and JSON
0:39
which is the web's most important transport type. If we want to create a method, that allows us to use keyword arguments,
0:46
one of the ways we can do that is to have the kwargs, **kwargs parameter and this allows us to not just pass a set of non-keyword arguments
0:55
but actually arbitrary ones as well, and those come through as a dictionary. As we'll see, dictionaries add incredible performance boost
1:03
for certain types of algorithms, and we'll look at that in the section as well. Python, the language does not have a switch statement,
1:10
and we don't miss it too often but sometimes a switch statement is really nice and you'll see that we can actually leverage dictionaries
1:17
to add switch statements, switch statement-like functionality to the Python language.
1:22
If you access a database and you are not using an ORM such is SQLAlchemy, typically the way the rows come back to you are each row comes back
1:30
either as a tuple or a dictionary, preferably as a dictionary so you can lookup the columns by name rather than index.
1:36
So that's just a taste of why dictionaries are so important in Python, you'll see there is a lot of cool Pythonic techniques around working with them,
1:42
so let's jump right in.