Question:
The problem is that I have a list "l"
from which I want to select a random item several times without repeating it, and I don't know how to do it. Any suggestion?
import random
l = ["a", "b", "c", "d"]
sel1 = random.choice(l)
So far so good, but now I want to define a "sel2"
that is a random element of "l"
but different from "sel1"
. And thus be able to continue defining a "sel3"
different from "sel1"
and "sel2"
.
Thanks in advance =)
Answer:
You could take another approach to your problem: why not unscramble the list and remove each item from it?
The shuffle
function allows you to shuffle the items in a list. Look at this example:
from random import shuffle
list = ["a", "b", "c", "d"]
shuffle(list) # list -> ["c", "a", "d", "b"]
sel1 = list[0] # "c"
sel2 = list[1] # "a"
....
Greetings.