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

Unlock This Lesson

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

Unlock This Lesson

Pick Three Random Nouns

00:00 In this lesson, I’ll start to pick out randomly some of the words, and I’m going to start off with the nouns. Going to pick out three nouns randomly.

00:10 Let’s see how we can do that. Oh, handles poetry. I missed an s. So we said that there’s a module called random that I can import.

00:21 And then that random module has a couple of functions that you can use to do pseudorandom stuff with Python.

00:29 Can I select three nouns at once? I think I can go ahead and probably have to write three lines of code, but let’s see if I can pick out one noun.

00:37 noun equals random.choice() And then I’ll pass in the nouns list. Because I wrapped this in a tuple, in a words tuple, I need to first go to the nouns list, and then I need to pass the nouns list in this case.

00:52 So I can say words[0], which is my nouns list, and that’s what I’m passing to the choice() function in the random module.

01:02 And then it’ll pick out one random noun. Let’s print it out just to see what it is doing, what we expect, and that it’s going to do different things every time we run it.

01:13 So I’ll press F5 to save and run, and I get out elephant. And then if I run it again, I get an eagle. Okay, it looks like what I want. There’s elephant and eagle, so it picks randomly from the nouns list.

01:31 Next on is moss. Cool. I like that. That’s working. I could probably continue working with that. With this, I pick out one word, and then I could just copy that and do it three times and call these noun one, two, and three.

01:48 However, one thing that might be a problem here is that I could potentially pick the same word twice. I don’t want that. I want each of the nouns to be unique and only appear once.

01:59 So what could we do? We could remove them from the list once I’ve actually picked it out. So I could say words[0].remove() to go to my nouns list.

02:14 And because this is a list, I can mutate it. I can use the .remove() method, which is maybe one that you haven’t covered yet, but you pass it a value from the list, and then it’ll remove the first occurrence of that value from the list, and it mutates the list.

02:28 So then it’s just going to be gone from that list. And since all of the words only appear once in there, that this removes the first occurrence isn’t an issue for me.

02:38 So I can pass in the randomly selected noun from the previous line and remove it from the list so that then when I run random.choice() the second time, it’s not possible that it’s going to pick that again because this is just not part of the list anymore.

02:53 So I could do that again down here with noun two and once more with noun three.

03:00 And let me print them out.

03:04 Noun one, noun two, noun three.

03:10 Let’s run that again. Mouse, gorilla, badger seems to work, but it’s also possible that it just didn’t pick the same ones twice. So let’s double-check the nouns list.

03:23 So my words[0] list. Looks like at the end of this piece of code that I wrote,

03:34 we get out horse, mouse, and sparrow. And the list that’s left over doesn’t contain any of these words anymore. So it just contains badger, artwork, gorilla, elephant, and eagle.

03:45 So that works. That’s a way that I could pick three different nouns randomly from that list. And then I can continue working with those.

Become a Member to join the conversation.