#100DaysOfCode in Python Transcripts
Chapter: Days 16-18: List comprehensions and generators
Lecture: Third day: solution and islice
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Welcome back. I hope yesterday's exercise was reasonable for you but starting today I will show you a possible solution. If it was very easy for you,
0:11
feel free to skip to the next video where I have some other exercises lined up for you. Okay, so the first thing we needed to do
0:18
was to title case the names using a list comprehension. That should be pretty easy now. So, name title for name in names. Oops and names is not defined
0:34
because I did not run the cell and let's run it again. Okay, cool. So, every name is title cased. And then we have to write a list comprehension,
0:43
reverse the first and the last name using a helper function. So, let's define reverse the first, last names
0:56
and it takes a name, split the name in first and last so this is a nice example of unpacking.
1:05
So the name splitted by defaults space, get you two elements and you can assign them directly to first and last.
1:14
Then, we return them and I was using a join but in 3.6 you can use f-strings where you can embed the variables, which is very nice.
1:29
And let's do the list comprehension to use that function. Reverse first, last names, name for name in names.
1:43
Right. And yeah, I dropped the title case requirement here but that worked. Then we move on to generators
1:49
and the exercise was to generate random pairs of names. So, name one teams up with name two, etc. First, define a function.
2:03
And, let's get the first names and we can again use a list comprehension for that. So, we split them again and we take the first element with indexing.
2:16
We title case that. That's nice with Python, that you can chain all these operations for name in names. So, let's do an infinite loop.
2:31
Which I usually do with while true. I initialize first and second. And this little while, I'll explain in a bit was that I had to add later.
2:52
And I used a random sample to take the first names list and pick two items. Why you needed the while? Well, it turned out that
3:02
I could have two teams of a Julian so the same name came out of random sample. So, while that's the case, keep picking two names basically.
3:11
So that was a little tweak I had to do to make sure that both names were always different. And then again, I used a f-string to return first,
3:22
teams up with second. And let's see if that works. So, I assign the generator two pairs. So for underscore in range and the underscore is just a way
3:42
in Python to say throw away variable I don't care really what that loop variable is. Print next pairs. I can adjust to four variable in 10 pairs
3:54
because that will go on infinitely. So I'm making sure I'm making next to retrieve one value at a time. Okay, I did not import random.
4:11
And there you go. Jewel teams up with Julian. Ali teams up with Bob, etc. One final thing I wanted to show you is itertools, islice
4:20
because I said before you can not just loop over an infinite generator, it will probably hang your system
4:25
because it never ends but islice, you can slice a generator just as you would slice a normal list but that overcomes
4:32
that problem, so I can just do, itertools.islice give it the generator and the number I want, that gives an islice object and
4:46
I can materialize those in a list by doing this. There you go. Okay, those were two possible solutions of the small exercises I gave you yesterday
5:00
and in the next video, I will show you some more exercises you can do today.