You can use random.sample()
if you want to pluck out three distinct elements from the list (i.e. foo
is never equal to bar
, which might not be what you want):
foo, bar, baz = random.sample(l, 3)
I renamed your variable to l
because list
is a built-in type. Overriding it by accident won't be pretty.
Here's a sample (no bad pun intended):
>>> import random
>>> random.sample(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3)
['g', 'c', 'e']