How can I get 6 unique values from the list below?
# List of donuts
Donuts = ['Apple Cinnamon',
'Strawberry","Custard',
'Sugar Ring',
'Chocolate Caramel',
'Lemon Circle',
'Blueberry Blaster',
'Strawberry Surprise',
'Simple Sugar']
This is what I have so far:
# Function - Random Selection of Donuts
def Generate_Random():
if (len(Donuts)>0):
choice.set(randint(0, len(Donuts)-1))
stvRandomChoice.set (Donuts[choice.get()])
You can do this very easily with random.sample(donuts, 6)
, which will return a list of 6 random elements from your list.
For more information see https://docs.python.org/3/library/random.html
Edit: Implemented for clarity:
import random
# List of donuts
Donuts = ['Apple Cinnamon',
'Strawberry',
'Custard',
'Sugar Ring',
'Chocolate Caramel',
'Lemon Circle',
'Blueberry Blaster',
'Strawberry Surprise',
'Simple Sugar']
# Function - Random Selection of Donuts
def Generate_Random(list, num):
return random.sample(list, num)
# Print out randomly selected donuts using the above function
for element in Generate_Random(Donuts, 6):
print element