Interviews - C# Corner

Dinesh Beniwal
How can you randomize the items of a list in place in Python?
By Dinesh Beniwal in Python onJun 20 2019
  • 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 Companies

Most Popular Job Functions

MOST LIKED QUESTIONS