Interviews2 answers
How can you randomize the items of a list in place in Python?
2 Answers
from random import shuffle
x = [[i] for i in range(10)]
shuffle(x)
Laxmidhar SahooPosted Jan 22, 2020, 3:10 PM EST
to randome a list, we use the shuffle method, which is a part of random python library
import randomnumber_list = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]print ("Original list : ", number_list)random.shuffle(number_list) #shuffle methodprint ("List after first shuffle : ", number_list)random.shuffle(number_list)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]
Rohit GuptaPosted Jul 14, 2019, 9:14 PM EDT



