OneCompiler

How to randomly print a element from a list in Python?

I have a list in Python as below

a = [1,2,3,4]

I want to print a single element randomly everytime from the list, how can I do that?

1 Answer

5 years ago by

You can use random package to generate a number between the range like below,

from random import randint as rnd
a = [1,2,3,4]
print(a[rnd(0,len(list)-1])
5 years ago by Divya