Python Random shuffle( ) method
The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of items like a list and Numpy array. The shuffle function cannot reorganize immutable data sets such as a string and tuple. This shuffle( ) method is generally used in password generators; we can randomly generate the password through this method. The shuffle( ) function does not return any value but changes the position of the elements in the mutable sequence.
Syntax :
random.shuffle(x, function)
Parameters:
- x-The x can be any mutable sequence of items such as a list and Numpy array.
- function- This is an optional parameter, and by default, it is set to random this must return a value between 0 and 1
Return: This function should not return any value, but it only changes the position of the elements in a mutable sequence.
Shuffling a List
The list can be rearranged by following the steps in python-
- Create a list-
We need to create a list in python using the list( ) function, or we can input the list manually
- Importing the random module-
We need to import the random module to shuffle the list randomly.
- Use shuffle( ) function –
Here, we use the random.shuffle(list) function to shuffle the list. This shuffle function does not return any value but changes the positions of the elements in the original list.
- Print the shuffled list-
As we know that the shuffle( ) function does not return any value, we need to use the print( ) function to display the shuffled list.
- Return the shuffled list instead of modifying the original list-
Generally, we require shuffled and original lists in many operations. So, we can make the copy of the original list using the copy ( ) function before shuffling then we can shuffle the list to get the shuffled list.
- Customize the shuffle if required-|
Generally, the function in the function in the shuffle is set to random. Still, we can also customize the function in place of random we can pass a custom function, which will pass the instructions to the shuffle( ) to randomize the list items.
Example-
import random
#original list
list=[4,5,23,45,67,89,12]
#displaying the original list
print(list)
#shuffling the list
random.shuffle(list)
#displaying the shuffled list.
print(list)
Output:
#original list-
[4, 5, 23, 45, 67, 89, 12]
#shuffled list-
[23, 67, 89, 4, 12, 5, 45]
Here, we can observe that the position of the elements is changed in the shuffled list after performing the shuffle( ) operation on the original list.
Shuffling Two Lists once in the Same Order
In this operation, we will see if we want to shuffle two lists and if we want to maintain the same shuffle order. Now let us consider an example of one list consisting of the student's names and the other list consisting of the student's marks; here, we will randomly shuffle both the lists by maintaining the same order.
Example
import random
# list consisting of names of students
students= [‘Nihaas’, ‘Nikitha’, ‘Bunty’, ‘Rohit’, ‘Rishika’, ‘Deeraj’, ‘Kavlas’]
#list consisting of marks obtained by students
marks= [96, 93, 84, 85, 95, 76, 80]
#displaying student's list before shuffling.
print(students)
#displaying marks list before shuffling.
print(marks)
#shuffling the two lists at once in the same order
mapIndexPosition = list(zip(students, marks))
random.shuffle(mapIndexPosition)
#making the lists separate
list_students, list_marks = zip(*mapIndexPosition)
#displaying lists after shuffling
print(list_students)
print(list_marks)
Output:
[‘Nihaas’, ‘Nikitha’, ‘Bunty’, ‘Rohit’, ‘Rishika’, ‘Deeraj’, ‘Kavlas’]
[96, 93, 84, 85, 95, 76, 80]
[‘Nikitha’, ‘Rohit’, ‘Deeraj’, ‘Bunty’, ‘Kavlas’, ‘Nihaas’, ‘Rishika’]
[93, 85, 76, 84, 80, 96, 95]
Shuffling a Numpy Multidimensional Array
To shuffle the numpy arrays first, we need to import the numpy library in which it has a numpy.random module to generate the random data using the numpy.random.shuffle( ) function, we can reorder the elements in the multidimensional array.
Example
import numpy as np
#Original shuffling two-dimensional array
original_array = np.arange(50, 130, 10)
original_array = original_array.reshape(4, 2)
print(original_array)
# shuffled two-dimensional array
np.random.shuffle( original_array )
print( original_array )
Output
#Original two-dimensional array
[[50 60]
[70 80]
[90 100]
[110 120]]
#Shuffled two-dimensional array
[[90 100]
[110 120]
[70 80]
[50 60]]
Here, we can observe that the elements in the numpy array are randomly reorganized, and the shuffled list is obtained.