In Python, Shuffling a sequence of numbers has always been a 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.
Python Random Shuffle a List
In Python, there are several ways to shuffle a list. Here are various Python ways for shuffling lists.
- Using sorted()
- Using random.shuffle()
- Using random.sample()
- Using the Random Selection Method
- Using Fisher-Yates shuffle Algorithm
- Using itertools.permutations() function
- Using NumPy
Random Shuffle a List using sorted()
A sorted version of the list can be produced using the sorted() function. We effectively shuffle the elements randomly by using them on a copy of the list that has been shuffled.
Python3
import random my_list = [ 1 , 2 , 3 , 4 , 5 ] shuffled_list = sorted (my_list, key = lambda x: random.random()) print ( "Original list:" , my_list) print ( "Shuffled list:" , shuffled_list) |
Output
Original list: [1, 2, 3, 4, 5]
Shuffled list: [2, 3, 4, 5, 1]
Time Complexity: O(nlogn), where n is the length of the list
Space Complexity: O(n), where n is the length of the list
Randomize a List using Random.Shuffle()
Random.Shuffle() is the most recommended method to shuffle a list. Python in its random library provides this inbuilt function which in-place shuffles the list. The drawback of this is that list ordering is lost in this process. Useful for developers who choose to save time and hustle.
Python3
import random test_list = [ 1 , 4 , 5 , 6 , 3 ] print ( "The original list is : " + str (test_list)) # using random.shuffle() to shuffle a list random.shuffle(test_list) print ( "The shuffled list is : " + str (test_list)) |
Output
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [5, 1, 3, 4, 6]
Time Complexity: O(n), where n is the length of the list
Space Complexity: O(n), where n is the length of the list
Randomize a List using Random.Sample()
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 the original list. This is useful in cases we require to retain the original list.
Python3
import random test_list = [ 1 , 4 , 5 , 6 , 3 ] print ( "The original list is : " + str (test_list)) # using random.sample()to shuffle a list res = random.sample(test_list, len (test_list)) print ( "The shuffled list is : " + str (res)) |
Output
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [4, 3, 1, 6, 5]
Time Complexity: O(n), where n is the length of the list
Space Complexity: O(n), where n is the length of the list
Randomize a List using Random Selection Method
In this method , we will randomize a list using random selection method . We select a index randomly and append that element at that index to the list.
Python3
import random arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] print ( "Original List: " , arr) n = len (arr) for i in range (n): j = random.randint( 0 , n - 1 ) element = arr.pop(j) arr.append(element) print ( "Shuffled List: " , arr) |
Output
Original List: [1, 2, 3, 4, 5, 6]
Shuffled List: [1, 5, 2, 6, 3, 4]
Time Complexity: O(n) where n is the length of the list
Space Complexity: O(1)
Random Shuffle a List using Fisher-Yates Shuffle Algorithm
This is one of the famous algorithms that is Fisher-Yates Shuffle Algorithm, mainly employed to shuffle a sequence of numbers in Python. This algorithm just takes the higher index value, and swaps it with the current value, this process repeats in a loop till the end of the list.
Python3
import random test_list = [ 1 , 4 , 5 , 6 , 3 ] 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] print ( "The shuffled list is : " + str (test_list)) |
Output
The original list is : [1, 4, 5, 6, 3]The shuffled list is : [3, 4, 5, 6, 1]
Time Complexity: O(n), where n is the length of the list
Space Complexity: O(n), where n is the length of the list
Randomize a List using Itertools.Permutations() Function
This method generates all possible permutations of the original list using the itertools.permutations() function, and then select a random one.
Python3
import random import itertools lst = [ 1 , 4 , 5 , 6 , 3 ] permutations = list (itertools.permutations(lst)) shuffled_lst = random.choice(permutations) print ( "Shuffled list:" , shuffled_lst) |
Output
Shuffled list: (6, 5, 4, 1, 3)
Time Complexity: O(n!) where n is the length of the list, due to the generation of all possible permutations.
Space Complexity: O(n!) as all possible permutations are generated and stored in a list.
Randomize a List using Numpy
We are using NumPy() to shuffle the items in the list. To randomize a list using numpy ,we have to convert list to NumPy array and then apply reduce function and it returns the shuffled list and Prints the shuffled list.
Python3
import numpy as np from functools import reduce test_list = [ 1 , 4 , 5 , 6 , 3 ] # Printing original list print ( "The original list is : " + str (test_list)) # using reduce() and numpy to shuffle a list res = reduce ( lambda acc, _: np.random.permutation(acc), range ( len (test_list)), np.array(test_list)) print ( "The shuffled list is : " + str (res.tolist())) |
Output
The original list is : [1, 4, 5, 6, 3]
The shuffled list is : [3, 6, 1, 5, 4]
Time Complexity: The time complexity of the reduce() function depends on the number of iterations, which is equal to the length of the list. The time complexity of np.random.permutation() is O(n) where n is the length of the input array. Therefore, the time complexity of this code is O(n^2).
Space Complexity: The space complexity of this code depends on the size of the list. The list is stored in memory along with a few additional variables used by the reduce() function. Therefore, the space complexity is O(n).