  Table of content

## How to shuffle a list in python?

In python, a shuffle list can be generated using the random module. The random module performs some actions randomly such as selecting random elements from a list, getting a random number, returns the sequence in a random order, etc.

The random.shuffle() method takes a sequence(list, tuple, set) as a parameter and shuffles the original list. The drawback of using this method is that the original list is lost. The syntax for shuffle() is given below.

``````#syntax:
random.shuffle(sequence)``````

Consider a list list_1 = [3, 4, 'red', 'apple']. random.shuffle(list_1) returns a shuffled list.

``````import random
list_1 = [3, 4, 'red', 'apple']
print("Original list_1: ", list_1)
random.shuffle(list_1)
print("Shuffled list_1: ", list_1)``````

Output

``````Original list_1:  [3, 4, 'red', 'apple']
Shuffled list_1:  [3, 4, 'red', 'apple']``````

The random.sample() method takes two arguments, a sequence(list, tuple, set), and the number of elements to return in the new shuffled list. The value for the second argument should be in the range of 0 to the length of the list.

The main difference between shuffle() and sample() is that the sample() method doesn't modify the original list. The syntax for sample() is given below

``````# syntax:
random.sample(sequence, num_ele)``````

Consider a list list_1 = [3, 4, 'red', 'apple']. random.sample(list_1, 3) returns 3 shuffled elements from list_1.

``````import random
list_1 = [3, 4, 'red', 'apple']
print("Original list_1: ", list_1)
shuffle_list = random.sample(list_1, 3)
print("Shuffled list_1: ", shuffle_list)``````

Output

``````Original list_1:  [3, 4, 'red', 'apple']
Shuffled list_1:  [4, 3, 'apple']``````

The Fisher-Yates shuffle algorithm generates a shuffled list. In this algorithm for loop iterates over the list. In each iteration a random index is generated from the unvisited indices value. The item at random index value will be swapped with the item at current index value. The algorithm for the Fisher-Yates shuffle is given below.

``````for i  from  n-1 to 1   # n is length of list
j = generate a random integer using randint() such that  0<= j < i
swap list[i] and list[j]``````

Consider a list list_1 = [3, 4, 'red', 'apple']. A for loop iterates over list_1. In each iteration the program generates a random index number using randint() function in the range of (i, len(list_1)-1) and stores in variable j.

The randint() method returns a random integer between a specified range [start, end]. The endpoints are included. The items in index i and j are swapped in each iteration.

``````import random
list_1 =  [3, 4, 'red', 'apple']
print("Original list_1: ", list_1)
for i in range(n-1, 0, -1):
j = random.randint(0, i)
list_1[i], list_1[j] = list_1[j], list_1[i]
print("Shuffled list_1: ", list_1)``````

Output

``````Original list_1:  [3, 4, 'red', 'apple']
Shuffled list_1:  [4, 'red', 'apple', 3]``````
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions