Randomly shuffle the list in python

In this tutorial we will be focusing on how to randomly shuffle the list in python. We will be trying out different ways to shuffle the list in python. one of them being using shuffle() method and other is to select elements in random and shuffle them using choice() function.

  • Randomly shuffle the list in python using shuffle() function.
  • Randomly shuffle the elements of the list in python using choices() function
  • Randomly shuffle the elements of the list in python using sample() function
  • shuffle the list in python with pop() and append() function
  • shuffle the list in python using index.
  • shuffle list to get the same list everytime using random.seed()

 

Shuffle-list-in-python

 

Randomly shuffle the list in python using shuffle() function:

We will be shuffling the list in python using shuffle() function. It takes the list as argument and shuffle them randomly as shown below.

# randomly shuffle the list in python with shuffle() function
import random
List = [1,1,4,3,2,4,1,2,3,4]
random.shuffle(List)
print (List)

or

# randomly shuffle the list in python with shuffle() function
from random import shuffle
List = [1,1,4,3,2,4,1,2,3,4]
shuffle(List)
print(List)

so, the output will be,

output:

[2, 1, 2, 1, 4, 3, 3, 4, 1, 4]

 

 

 

 

Randomly shuffle the elements of the list in python using choices() function

We will be shuffling the list in python using choices() function. choices() function takes the list as argument and length of the list to be shuffled. As choices() function randomly returns the all the elements of the list which in turn equals to shuffling list randomly as shown below.


# randomly shuffle the list in python with choices() function
import random
	
List = [1,1,4,3,2,4,1,2,3,4]
  
# using the choices() method on the 
# given dataset
randomList = random.choices(List, k = len(List))
  

print(randomList)

so, the output will be,
output:

[1, 1, 3, 3, 1, 1, 4, 2, 4, 1]

 

 

 

Random shuffle the list in python using sample() function:

We will be shuffling the list in python using sample() function. sample() function takes the list as argument and length of the list to be shuffled. As sample() function randomly returns the all the elements of the list which in turn equals to shuffling list randomly as shown below.


# rondomly shuffle the list in python with random.sample() function
import random

List = [1,1,4,3,2,4,1,2,3,4]
  

randomList = random.sample(List, len(List))

print(randomList)

so, the output will be,
output:

[1, 4, 2, 4, 3, 4, 3, 2, 1, 1]

 

 

 

Random shuffle the list in python with pop() and append() function:

We will be shuffling the list in python using pop() and append() function. We will be selecting the index randomly and deleting the element at that index and then append the deleted element to the list, which in will in turn shuffle all the elements in the list in a roundabout way as shown below.


# # randomly shuffle the list in python with pop() and append() function

import random
# Assign array
List = [1,1,4,3,2,4,1,2,3,4]
 
# Display original array
print("Original List: ", List)
 
# Get length of List
n = len(List)
 
#repeat the following for n number of times
for i in range(n):
    #select an index randomly
    j = random.randint(0, n-1)
    #delete the element at that index.
    element=List.pop(j)
    #now append that deleted element to the list
    List.append(element)
print("Shuffled List: ",List)

so, the output will be,

output:

Original List: [1, 1, 4, 3, 2, 4, 1, 2, 3, 4]
Shuffled List: [1, 1, 2, 3, 4, 4, 2, 3, 4, 1]

 

 

 

Randomly shuffle the list in python using index:

We will be using the fisher -Yates shuffle algorithm to shuffle the list in python. 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. Which will result in the shuffled list as shown below.


import random
 
# initializing list
List = [1,1,4,3,2,4,1,2,3,4]
 
# Printing original list
print ("The original list is : " + str(List))
 
# using Fisher–Yates shuffle Algorithm
# to shuffle a list
for i in range(len(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
    List[i], List[j] = List[j], List[i]
     
# Printing shuffled list
print ("The shuffled list is : " +  str(List))

so, the output will be,
output:

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

 

 

 

Shuffle list to get the same list every time using random.seed():

random.seed() function will make sure that when refreshed or rerun every time, It will result in the same list. In the below code we have shuffled and generated 3 list. Each time this 3 list will be repeated when we use the same argument inside seed() function as shown below


import random
numbers = [1,1,4,3,2,4,1,2,3,4]
print("Original list: ", numbers)

# shuffle 3 times
for i in range(3):
    # seed PRNG
    random.seed(4)
    # shuffle list
    random.shuffle(numbers)
    print("result list ", numbers)

so, the output will be,
output:

Original list: [1, 1, 4, 3, 2, 4, 1, 2, 3, 4]
result list [3, 4, 1, 2, 1, 4, 4, 1, 2, 3]
result list [2, 1, 3, 1, 4, 3, 4, 4, 1, 2]
result list [1, 3, 2, 4, 4, 2, 3, 1, 4, 1]

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    https://www.linkedin.com/in/sridhar-venkatachalam-74414252/ karaimadai@sg2plcpnl0204.prod.sin2.secureserver.net Venkatachalam Sridhar