#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.
0:01 I hope yesterday's exercise was reasonable for you
0:04 but starting today I will show you a possible solution.
0:08 If it was very easy for you,
0:10 feel free to skip to the next video
0:12 where I have some other exercises lined up for you.
0:15 Okay, so the first thing we needed to do
0:17 was to title case the names using a list comprehension.
0:22 That should be pretty easy now.
0:24 So, name title for name in names.
0:31 Oops and names is not defined
0:33 because I did not run the cell and let's run it again.
0:37 Okay, cool. So, every name is title cased.
0:40 And then we have to write a list comprehension,
0:42 reverse the first and the last name
0:45 using a helper function.
0:47 So, let's define reverse the first, last names
0:55 and it takes a name, split the name in first and last
1:02 so this is a nice example of unpacking.
1:04 So the name splitted by defaults space, get you two elements
1:09 and you can assign them directly to first and last.
1:13 Then, we return them and I was using a join but in 3.6
1:18 you can use f-strings where you can embed the variables,
1:21 which is very nice.
1:28 And let's do the list comprehension to use that function.
1:31 Reverse first, last names, name for name in names.
1:42 Right. And yeah, I dropped the title case requirement here
1:45 but that worked.
1:47 Then we move on to generators
1:48 and the exercise was to generate random pairs of names.
1:53 So, name one teams up with name two, etc.
1:57 First, define a function.
2:02 And, let's get the first names and we can again
2:06 use a list comprehension for that.
2:11 So, we split them again
2:12 and we take the first element with indexing.
2:15 We title case that. That's nice with python,
2:19 that you can chain all these operations for name in names.
2:27 So, let's do an infinite loop.
2:30 Which I usually do with while true.
2:34 I initialize first and second.
2:42 And this little while, I'll explain in a bit
2:45 was that I had to add later.
2:51 And I used a random sample to take the first names list
2:57 and pick two items.
2:59 Why you needed the while? Well, it turned out that
3:01 I could have two teams of a Julian
3:04 so the same name came out of random sample.
3:06 So, while that's the case, keep picking two names basically.
3:10 So that was a little tweak I had to do to make sure
3:13 that both names were always different.
3:15 And then again, I used a f-string to return first,
3:21 teams up with second.
3:26 And let's see if that works.
3:28 So, I assign the generator two pairs.
3:35 So for underscore in range and the underscore is just a way
3:41 in Python to say throw away variable
3:43 I don't care really what that loop variable is.
3:46 Print next pairs. I can adjust to four variable in 10 pairs
3:53 because that will go on infinitely.
3:56 So I'm making sure I'm making
3:57 next to retrieve one value at a time.
4:04 Okay, I did not import random.
4:10 And there you go. Jewel teams up with Julian.
4:12 Ali teams up with Bob, etc.
4:16 One final thing I wanted to show you is itertools, islice
4:19 because I said before you can not just loop over
4:21 an infinite generator, it will probably hang your system
4:24 because it never ends but islice, you can slice a generator
4:28 just as you would slice a normal list but that overcomes
4:31 that problem, so I can just do, itertools.islice
4:39 give it the generator and the number I want,
4:42 that gives an islice object and
4:45 I can materialize those in a list by doing this.
4:52 There you go. Okay, those were two possible solutions
4:56 of the small exercises I gave you yesterday
4:59 and in the next video,
5:01 I will show you some more exercises you can do today.