Exercise 41 - does random.sample() pr definition make a list?

I’m looking at the following code in Exercise 41:

    class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count("%%%"))]
    print(f'class_names: {class_names}')

Output: []

    other_names = random.sample(WORDS, snippet.count("***"))
    print(f'other_names: {other_names}')

Output: [‘color’, ‘discussion’, ‘distribution’]

I understand how the first bit of code becomes a list as the code is embedded betweeen [].
How does the second code block become a list?
Is it because random.sample() pr definition makes a list?

If I assume that random.sample pr definition makes a list is the list comprehension then only there because of w.capitalize()?

Yes, random.sample should make a new list. The first one is just capitalizing the random words so they look like classes.

1 Like