Computer >> Computer tutorials >  >> Programming >> Python

How to randomly select an item from a tuple in Python?


The choice() function is useful for this purpose. It returns on item randomly selected from any sequence object (tuple in this case)

>>> import random
>>> t1=(11,'aa',12.50,77,'xyz')
>>> item=random.choice(t1)
>>> item
11
>>> item=random.choice(t1)
>>> item
'xyz'