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:04 revolving around dictionaries in Python.
0:06 So, the first question you might ask is like
0:09 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:26 which is a dictionary for the fields and what not 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:36 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:49 **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:59 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 odes not have a switch statement,
1:10 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:19 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.