Dinesh Beniwal
How can you randomize the items of a list in place in Python?
By Dinesh Beniwal in Python on Jun 20 2019
  • Laxmidhar Sahoo
    Jan, 2020 22

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

    • 0
  • Rohit Gupta
    Jul, 2019 15

    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]

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS