Jump to content

Python Random Shuffle Method


Linux Hint

Recommended Posts

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

Python-Random-Shuffle-Method-1.gif

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

Python-Random-Shuffle-Method-2-1.png

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

Python-Random-Shuffle-Method-3-1.png

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

Python-Random-Shuffle-Method-4-1.png

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...