Hey there, Michael here again for another Python Shorts, where we learn something amazing
in a couple of minutes about Python in this one we're going to talk about comprehensions. We've
talked about list comprehensions and you may have heard of it. That's the most common way to create
liners that generate lists with interesting transforms and filters, but did you know you can do
that for other data structures like dictionaries and sets? You will in the next couple of minutes.
Here we are on PyCharm. Let's jump in. So we've got a new bit of code. You may have seen the
loops to comprehension short if you haven't I'll put a link to that video up there. In this
one what we did is we loaded up a list of data and here we have a list of this class and this
class has things like an id names, emails and it's modeling buying cars or customers of a car company.
The make, the model, the year, the van and so on and we've got this mock data where it's coming
from, for example, del cashmore with that email address. Let's copy that we're going to use it
in a second bought this car with this Vin number for that price, so all of that is already in place
we're already loading that data up here. We have about a thousand entries in that file, exactly a
thousand, what we want to do is we want to take and create other data structures from this list
of those customers, you can see the type right there what we want is a dictionary we'd like
to be able to say given an email instantly find near instantly find the associated customer with
that email given a Vin number with near instant response time give us that customer that bought
that car with that Vin number and finally we want to take and find all the unique Vin numbers we've
had customers who have bought a car and they sold it back, and we sold it to another customer
so in that list of a thousand sales we don't have a thousand unique cars. We have something like 990
or something along those lines, so let's go ahead and do that and then once we build up these data
sets. We ask simple questions like given an email we type in use the dictionary to do a super fast
query there and there and then we're just going to print out the unique, then numbers remember sets
store items without duplication. So what we're going to do is put just the Vin numbers in there
and if there's duplicates it'll compress it down all right, so here's how it works
we've seen for list comprehensions that we had something like this. We had final item for item
in data. Let's do it like this data 'if' test and that was a list comprehension right there okay
what we want is a dictionary, now it's very similar for the dictionary, we do curly braces instead
and it's like this except for we have key colon final item right and it's this
curly braces here and this divider of the colon, so let's go and use that. Now here's a tip if i
go over and say I want the key. We're going to have the sale so we may call it s we say s.
you get no help but watch this if we write the last part first 'for s in' sales and then go back
s . oh yeah, there's that help. We're looking for email and where we want in there, we want the
key for each entry is going to be an email and then the thing we want back is the actual sale
let's do the same thing for the Vin number it's incredibly similar
but instead of using the key as the email, I'm going to save in and let's go ahead and run it
just for those first two and notice it's going to say do a search by email and let's go back to
our mock data and we'll pull up. Let's go down in the middle somewhere. That's good check out this
person right here, so we enter their email address. Look at that Kim's back Volkswagen
new Beatles such-and-such for that with that Vin number for that price. How awesome is that
we also can search by Vin number so we should get a Silverado 2500 back with that Vin number
and we do so cool. Now the last thing is it's going to show us how many items are
these unique Vin numbers, so let's go do that we built up with this syntax as our dictionary
comprehension. Now the sets are suspiciously similar except for instead of going key colon
value. They just have the item or final item i guess I'm calling it like this, so we're gonna
do almost the same thing here for the Vin like this and instead of having the key structure
the key value. We just have the keys all right one more time. Let's go search by email perfect that
person bought the Silverado. We could also search by Vin number and then we hit enter
and there we have all the Vin numbers if I were to wrap that around. We do the word wrap
you'll see there's a bunch of them, but we only have 994 neat cars out of those thousand sales
pretty awesome, so here's how we can go beyond list comprehensions which is this one with dictionary
and set comprehensions again. We're doing an incredible amount of cool work in just three lines
of code here you can see we built this nice mini in-memory database like thing super super cool
You think this is interesting? Do you find it useful use it in your own code. Tell us how down
in the comments and while you're there, please like the video, it really helps support our work
Now you know there are more comprehensions than just the list comprehension. We have the dictionary
comprehension and the set comprehension and on top of this, we also have generators that will
extend the way we build the list. So many cool ways to take these little expressions and build
up amazing Python data structures by using them hopefully you enjoyed that. If you want to get more
click on my face up there and subscribe to the channel. You also check out talk
python to me, the podcast, where we talk about these ideas a lot and we have hundreds of
hours of Python courses over at talk python training, so there's two more resources if
you want to go deeper. Hopefully you enjoyed the video. See you next time.