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

How to shuffle a list of objects in Python?


Python has a random module which provides randomization related functions. The shuffle() function randomizes the sequence of items in a list.

>>> import random
>>> l1=['aa',22,'ff',15,90,5.55]
>>> random.shuffle(l1)
>>> l1
[22, 15, 90, 5.55, 'ff', 'aa']
>>> random.shuffle(l1)
>>> l1
['aa', 'ff', 90, 22, 5.55, 15]