Python | Ways to shuffle a list

Shuffling a sequence of numbers have always been an useful utility and the question that has appeared in many company placement interviews as well. Knowing more than one method to achieve this can always be a plus. Let’s discuss certain ways in which this can be achieved.

Method #1 : Fisher–Yates shuffle Algorithm

This is one of the famous algorithms that is mainly employed to shuffle a sequence of numbers in python. This algorithm just takes the higher index value, and swaps it with current value, this process repeats in a loop till end of the list.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# shuffle a list 
# using Fisher–Yates shuffle Algorithm
  
import random
  
# initializing list 
test_list = [1, 4, 5, 6, 3]
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# using Fisher–Yates shuffle Algorithm
# to shuffle a list
for i in range(len(test_list)-1, 0, -1):
      
    # Pick a random index from 0 to i 
    j = random.randint(0, i + 1
    
    # Swap arr[i] with the element at random index 
    test_list[i], test_list[j] = test_list[j], test_list[i] 
      
# Printing shuffled list 
print ("The shuffled list is : " +  str(test_list))

chevron_right


Output:

The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [4, 3, 1, 5, 6]

 
Method #2 : Using random.shuffle()
This is most recommended method to shuffle a list. Python in its random library provides this inbuilt function which in-place shuffles the list. Drawback of this is that list ordering is lost in this process. Useful for developers who choose to save time and hustle.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# shuffle a list 
# using random.shuffle()
  
import random
  
# initializing list 
test_list = [1, 4, 5, 6, 3]
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# using random.shuffle()
# to shuffle a list
random.shuffle(test_list)
  
# Printing shuffled list 
print ("The shuffled list is : " +  str(test_list))

chevron_right


Output:

The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 6, 4, 3, 1]

 
Method #3 : Using random.sample()
This is quite a useful function, better than the shuffle method used above in aspect that it creates a new shuffled list and returns it rather than disturbing the order of original list. This is useful in cases we require to retain the original list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# shuffle a list 
# using random.sample()
  
import random
  
# initializing list 
test_list = [1, 4, 5, 6, 3]
  
# Printing original list 
print ("The original list is : " + str(test_list))
  
# using random.sample()
# to shuffle a list
res = random.sample(test_list, len(test_list))
  
# Printing shuffled list 
print ("The shuffled list is : " +  str(res))

chevron_right


Output:

The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 3, 6, 1, 4]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.