Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Use a List Comprehension

00:00 For the sake of fun and exercising, let’s try out a different approach for getting the three random adjectives.

00:09 You may know that if you’re doing some sort of loop where you’re just creating a list and appending to that list inside of your loop, then there’s a chance that you can replace it with a list comprehension.

00:20 And while the code that you wrote before doesn’t entirely work for that because you’re doing some sort of removal task in there, at least with the specific code that you’re using here, it’s not a great fit for list comprehension.

00:33 We could probably use some slightly different functions and methods to make that work. So I want to try that out. Now to do the three adjectives using a list comprehension, the basics of a list comprehension is you give it a name.

00:51 So I’m going to call this one adjectives because eventually I want to get that adjectives list

00:56 and start off with the open and close square brackets. And now in here, I want to start with the element that I want to add to the list in this case. And up here with .remove(), that wouldn’t work out, but if you recall, there is a method that returns the element that you pick out of the list.

01:15 And that method is .pop().

01:19 And if you don’t pass an argument to it, then it’s going to take the last element of the list.

01:25 I need to call this method on a list object. And we have that list object for adjectives. We know it’s, it’s at words index number two, nouns is zero, verbs is one, adjectives is two, words[2] is a reference to my adjectives list.

01:43 And now I’m popping off the last element of that list. So this is what I want to do. It’s not entirely done yet, right? So we need to first finish this comprehension, and I’ll keep with the idea that I’m using up here for the number, like just the amount of times I want to do it by saying for _ in range(3), I’m popping off three elements from the adjectives list.

02:06 However, currently, because I’m not passing any index in here, it’s always just going to get the final element. So that would mean that every run that I do, I’m going to get the same adjectives.

02:18 Let’s actually try it out. I’m going to get rid of these for now. And then just print out the three adjectives.

02:31 Give it a go. I’m going to save and run this. Oops.

02:37 We get serene, melancholic, and glistening. And now if I run this again, it’ll be the same words every time I run it because I’m just popping off the final three of that adjectives list.

02:49 So also, if you look here all the way over there, you see it’s glistening, melancholic, and serene.

02:54 That’s not quite a solution yet because I want random ones, but we do have the list comprehension, so that’s kind of fun. To solve the random part, we’re going to again grab a function from the random module, and it contains a function that’s called shuffle() and shuffle() takes, let me see what IDLE says here.

03:14 shuffle() takes a list as an input and shuffles the elements in that list.

03:19 And then there’s an optional parameter that I’m not even going to use. So I will shuffle words[2], which is again, a reference to the adjectives list.

03:31 Then I will pop out the last three elements from there. And so every time this gets shuffled, there’s going to be a different order. And then, so it’s fine that I just take the last three because there’s going to be different last three ones every time I run this.

03:46 Let’s give it a try. Okay, we’re getting glistening, incredulous, and melancholic,

03:54 and if I run this again, we get fragrant, serene, and melancholic. It shouldn’t always do melancholic. That just happened this time. So incredulous, exuberant, and fragrant.

04:05 You can see this is a different approach that I could have used for both of these nouns and verbs situations. Before we have the exact same situation, I need to get three random words out of a list, and I want to make sure that it’s not the same ones up here.

04:20 I solved it with a for loop and then random.choice() and the .remove() list method. And down here now I solved it with using random.shuffle(), shuffling the list, and then using a list comprehension where I use .pop() to pick off the final three elements of the shuffled list and assigning it then to the adjectives list.

04:41 So a fun different way of doing it.

04:45 Now we have two prepositions. Let’s do that in the next lesson, and let’s try yet another approach.

Become a Member to join the conversation.