Selecting an item at random from Python list

random.choice(seq)
This return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

Example

>>> import random
>>> anyList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(random.choice(anyList))
4
>>> print(random.choice(anyList)) # Another random selection
5
>>>
Share

You may also like...