Is there a command to randomly pick an item of a list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a command to randomly pick an item of a list?

26th Mar 2020, 3:28 PM
Aman Muhammed
Aman Muhammed - avatar
2 Answers
+ 2
# Of course! import random lst = [1,2,3,4,5] print(random.sample(lst,3)) # The 3 in the parenthesis means it'll pick 3 random elements from the list
26th Mar 2020, 3:32 PM
BlaBlaBla BlaBla
0
Yes, but it needs to be imported from random module. from random import choice lst = [1,2,3,4,5] print(choice(lst)) #2 (for example)
26th Mar 2020, 3:33 PM
Russ
Russ - avatar