How to shuffle a list of objects in Python?


In this article, we will show you how to shuffle a list of objects in python. Below are the various methods to accomplish this task:

  • Using random.shuffle() function

  • Using random.sample() function

  • Using Fisher–Yates shuffle Algorithm

  • Using random.randint() and pop() function

Assume we have taken a list containing some elements. We will shuffle the elements of the list randomly using different methods as specified above.

Using random.shuffle() function

The shuffle() method in the random module is used to shuffle a list. It takes a sequence, such as a list, and reorganizes the order of the items.

This shuffle() method changes the original list, it does not return a new list. The ordering of lists is lost in this process which is the only drawback of this method.

Syntax

random.shuffle(sequence, function)

Parameters

  • sequence - any sequence like a list, tuple, etc.

  • function(optional)- a function's name that returns a value between 0.0 and 1.0. The function random() will be used if it is not specified.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the random module.

  • Create a variable to store the input list.

  • Print the input list.

  • Use the random.shuffle() function to shuffle all the list elements randomly by passing the list as an argument to it.

  • Print the resultant shuffled list.

Example

The following program returns the shuffled list using random.shuffle() function–

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # shuffling the list of elements using the random module shuffle function random.shuffle(inputList) # Printing the shuffled list print ("Shuffled list: ", inputList)

Output

On executing, the above program will generate the following output –

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 3, 10, 2, 5]

Using random.sample() function

The random.sample() method in python returns a new shuffled list. The original list remains unchanged.

The random.sample() method returns a list containing a randomly selected number of elements from a sequence.

Syntax

random.sample(sequence, k)

Parameters

  • sequence - any sequence like a list, tuple, etc

  • k - number of elements to return

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the random module

  • Use the random.sample() function to shuffle all the list elements randomly by passing the input list, and length of an input list using the len() function(The number of items in an object is returned by the len() method) as arguments.

  • Print the resultant shuffled list.

Example

The following program returns the shuffled list using random.shuffle() function–

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # shuffling the list elements using the sample function shuffledList = random.sample(inputList, len(inputList)) # Printing the shuffled list print ("Shuffled list: ", shuffledList)

Output

On executing, the above program will generate the following output –

Input list: [3, 10, 5, 9, 2]
Shuffled list: [2, 5, 9, 10, 3]

Using Fisher-Yates shuffle Algorithm

This is a well-known algorithm in Python that is used to shuffle a sequence of numbers.

Fisher-Yates shuffle Algorithm

The Fisher-Yates shuffle Algorithm has a time complexity of O(n). The assumption is that we are given the function rand(), which generates a random number in O(1) time. The idea is to begin with the last element and swap it with a randomly chosen element from the entire array (including the last). Consider the array from 0 to n-2 (with the size reduced by one) and repeat the process until we reach the first element.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the for loop to traverse from the end of the list with the len() function (returns the number of items in an object)

  • Get the random index till the current index value using the random.randint() method(Returns a random number within the specified range)

  • Swap the current index element with the element at a random index

  • Print the resultant shuffled list.

Example

The following program returns the shuffled list using Fisher-Yates shuffle Algorithm –

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print ("Input list: ", inputList) # traversing from the end of the list(In reverse order) for p in range(len(inputList)-1, 0, -1): # getting a random index from 0 to the current index q = random.randint(0, p + 1) # Swap the current index element with the element at a random index inputList[p], inputList[q] = inputList[q], inputList[p] # Printing the shuffled list print ("Shuffled list: ", inputList)

Output

On executing, the above program will generate the following output –

Input list: [3, 10, 5, 9, 2]
Shuffled list: [9, 10, 3, 5, 2]

Using random.randint() and pop() function

random.randint() − Returns a random number within the specified range

Example

The following program returns the shuffled list using random.randint() and pop() function –

# importing random module import random # input list inputList = [3, 10, 5, 9, 2] # Printing the input list print("Input list: ", inputList) # getting the list length listLength = len(inputList) # repeating the loop till the length of the list for i in range(listLength): # getting a random index in the range 0 and list Length - 1 randomIndex = random.randint(0, listLength-1) # deleting the element at that corresponding index from the list ele= inputList.pop(randomIndex) # appending the above-deleted element to the input list(adding the element at last) inputList.append(ele) # Printing the shuffled list print ("Shuffled list: ", inputList)

Output

On executing, the above program will generate the following output –

Input list: [3, 10, 5, 9, 2]
Shuffled list: [10, 2, 3, 5, 9]

Conclusion

In this article, we learned four different Python methods for shuffle the given list. We also learned about the Fisher-Yates algorithm for shuffled lists and how to use it in Python.

Updated on: 28-Oct-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements