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

Talk Python's Mastodon Michael Kennedy's Mastodon