| by Arround The Web | No comments

Python Random Shuffle Method

In Python, different modules and functions are used to execute certain operations or tasks. To generate random integer numbers or to generate random list elements, the “random” module is used in Python. After generating a random list, we sometimes need to shuffle the list. For this task, the “random.shuffle()” method of the “random” module can be used.

This Python write-up will explain a thorough guide on the Python “random.shuffle()” method using numerous examples and via the following content:

What is the “random.shuffle()” Method in Python?

The “random.shuffle()” method accepts a sequence, such as a list or a tuple, and reshuffles the element order.

The syntax of the “random.shuffle()” method is shown below:

random.shuffle(sequence)

 

The “random.shuffle()” method changes/modifies the input list without returning a new list.

Example 1: Shuffling a List Using “random.shuffle()” Method

To shuffle a list, the “random.shuffle()” method is utilized in the following code:

import random
list_value = ["Joseph", "Lily", "Anna"]
random.shuffle(list_value)
print(list_value)

 

In the above code:

  • The module named “random” is defined/imported, and the “list” is declared.
  • The “random.shuffle()” method accepts the “list” as an argument/attribute and rearranges the list element.

Output

The list has been shuffled successfully.

Example 2: Shuffling a List of Integers Using “random.shuffle()” Method

Let’s examine the following code:

import random
list_value = [4, 5, 3, 1, 7]
random.shuffle(list_value)
print(list_value)

 

Here in this code:

  • The “random” module is imported, and the list of integers is initialized.
  • The “random.shuffle()” takes the list of integers and shuffles the list element.

Output

Example 3: Shuffling NumPy Multidimensional Array Using “np.random.shuffle()” Method

The following code is used to shuffle Numpy multidimensional array using the “np.random()” method:

import numpy
arr1 = numpy.arange(2, 20, 2)
arr1 = arr1.reshape(3, 3)
print('Given 2-D Array: \n', arr1)
numpy.random.shuffle(arr1)
print('After shuffling 2-D Array: \n',arr1)

 

In the above code:

  • The “numpy” module is imported.
  • The “numpy.arange()” method is used to create a 2-D array, and “reshape()” is used to reshape the array in a “3×3” array.
  • The “numpy.random.shuffle()” method is employed to shuffle the array, indicating that the order of the array’s items is changed.

Output

The “2-D” Numpy array has been shuffled successfully.

Alternative Method to Shuffle a List

We can also utilize the “random.sample()” method of the random module to rearrange a list:

import random
l1 = [2, 5, 3, 6, 7, 4]
print("Original List: \n",l1)
print("\nShuffled List: \n", random.sample(l1,len(l1)))

 

In the above code block:

  • The “random” module is defined, and the list is created and displayed to the console.
  • The “random.sample()” accepts the input list and the length of the input list as an argument and rearranges the list.
  • The “random.sample()” function will randomly select a subset of the list of the specified length.

Output

The input list has been shuffled successfully.

Conclusion

The “random.shuffle()” method of the “random” module is used to shuffle a list of strings or a list of integers in Python. The “np.random.shuffle()” method of the “numpy” module can be used to shuffle a NumPy multidimensional array. Alternatively, the “random.sample()” method can also be used to shuffle a list in Python. This write-up delivered a comprehensive blog on the “random.shuffle()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply