How to randomize the items of a list in Python?



The random module in the Python standard library provides a shuffle() function that returns a sequence with its elements randomly placed.

>>> 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]

Advertisements