2
Reply

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

Dinesh Beniwal

Dinesh Beniwal

5y
2.7k
2
Reply

    from random import shuffle x = [[i] for i in range(10)] shuffle(x)

    to randome a list, we use the shuffle method, which is a part of random python library

    1. import random
    2. number_list = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
    3. print ("Original list : ", number_list)
    4. random.shuffle(number_list) #shuffle method
    5. print ("List after first shuffle : ", number_list)
    6. random.shuffle(number_list)
    7. print ("List after second shuffle : ", number_list)

    OUTPUT

    Original list : [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]

    List after first shuffle : [7, 14, 56, 21, 35, 70, 49, 42, 28, 63]

    List after second shuffle : [35, 63, 70, 21, 42, 28, 49, 56, 14, 7]