Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 10: Movie Search App
Lecture: Exploring the search API: The Pythonic Version

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's return to how we're parsing these movies here.
0:04 What we're doing is we're creating this movie list
0:06 we're looping over all the results, we're allocating one of these,
0:09 and then we're adding it here, let's address this part first.
0:13 So it turns out that this keyword value keyword value thing that we're doing
0:18 actually has a shortcut, a simplified version,
0:21 when you're working with dictionaries as the source,
0:24 and this md is a dictionary, we'll be able to condense this down
0:28 assuming that the keywords here and the keys in the dictionary
0:33 that it's coming from actually line up exactly,
0:36 if that's the case, you can do what I’m bout to show you.
0:39 So let me save this, let me make a copy of this and we'll try another one,
0:42 so quick diversion so you guys understand what's going on here,
0:46 suppose I have a method or function and it has
0:49 some positional parameters, so like x y and z, right,
0:53 if I'm going to call this method, I would say seven, one
0:58 and I could even say z = 2, right
1:01 so I could either call it positionally like x and y
1:04 or I could call this function using the keyword named parameter thing.
1:08 Some methods are extensible, so I could say format = true, age = seven
1:16 but you can see, PyCharm is saying no, no, this is not going to work;
1:19 so in order to make this sort of extensible,
1:22 to say look you can pass other stuff and maybe we can work with it
1:25 we're going to use this concept that's often called
1:28 in the variable kwargs, keyword arguments.
1:31 Now this name here is just a convention,
1:34 it could be jerry it could be anything, it doesn't matter,
1:37 the symbol that we use is this ** here,
1:41 so if I print out kwargs, like that, if I run this, let's see what we get;
1:48 you can see kwargs its format is true and age as seven
1:53 like this extra step that we passed here, under **kwargs,
1:57 becomes, you can see the curly braces- a dictionary.
2:00 So, with that in mind, there's a way to reverse this,
2:04 so what we're doing here is we're taking keyword arguments
2:07 and we're turning them into a dictionary,
2:10 but we can use in a reversed direction we can use this **
2:13 to go from a dictionary to keyword arguments,
2:16 so what we can do down here is we can replace all of this
2:19 with just going to the dictionary that has the data md
2:23 and we can say ** md, and what that means basically is
2:28 exactly what we had written besides little zeros,
2:31 besides this little part right here where I was saying alternate default values
2:35 but this is what it means, it means go here and get all the values out
2:39 and apply them as keyword arguments.
2:42 So if I run this, it should run exactly the same,
2:45 if I search for 'runner' we should see things like Blade Runner and so on.
2:49 So this is pretty cool, again I said it's only going to work
2:53 if the keyword arguments or the arguments expected
2:56 exactly match up with the keys, if there is
2:59 one is capital title one is lower case title this isn't going to work.
3:03 This is really, really nice. Now, let's do one more thing.
3:07 We can improve it one more time,
3:10 we can come down here and whenever you want to
3:13 create a list and then loop over something,
3:15 and then put some sort of transformed or computed element into the list like this,
3:20 we can do this with a list comprehension.
3:24 So, we saw that before, we can come down here and just say this
3:27 maybe write like this for md in movie list, and what are we going to put in,
3:36 we want to put in this movie result thing,
3:39 so taking the md and converting it to that, there.
3:42 Now, this should run more or less identical, the results should be identical,
3:46 so if we go down here for runner again, we get exactly the same results.
3:52 So look at how amazing this is, look how short and clean and concise,
3:56 we want to go to the movie list and convert it into a list of movie results
4:01 by assigning all the values that are in the dictionary,
4:05 compare that to this big puppy, this one is more flexible
4:09 and maybe you need to use this form like I said
4:12 if the keys don't match up or things like that,
4:14 but I think this is really beautiful and we'll use that in this particular app.