/  Technology   /  Python   /  How can you randomize the items of a list in place of Python?
python1(i2tutorials.com)

How can you randomize the items of a list in place of Python?

The method shuffle() can be used to randomize the items of a list in place. It should be noted that this function is not accessible directly and therefore we need to import or call this function using random static object.

Syntax:   shuffle (lst)

Here, ‘lst’ is passed as a parameter which could be a list or tuple. The shuffle() returns a reshuffled list of items.

Example:        

import random

list = [20, 16, 10, 5];

random.shuffle(list)

print "Reshuffled list : ",  list

random.shuffle(list)

print "Reshuffled list : ",  list

Output : 

Reshuffled list :  [16, 10, 5, 20]

Reshuffled list :  [5, 20, 10, 16]

Leave a comment