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