Python Random shuffle() Method with Examples

Random shuffle() Method in Python:

The shuffle() method reorganizes the order of items in a sequence, such as a list.

Note: Please keep in mind that this method modifies the original list and does not return a new list.

Syntax:

random.shuffle(sequence, function)

Parameters

sequence: This is Required. It could be any sequence.

function: This is Optional. The name of a function that produces a value between 0.0 and 1.0.
If no function is specified, the random() function will be used.

Return Value: This method produces no output.

Examples:

Example1:

Input:

Given List = ["good", "morning", "Btechgeeks"]

Output:

Given list After shuffling : ['Btechgeeks', 'morning', 'good']

Example2:

Input:

Given List = [10, 20, 30, 40]

Output:

Given list After shuffling : [20, 40, 30, 10]

Random shuffle() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list to the random.shuffle() method that reorganizes the order of items in a given list.
  • Print the above-given list after shuffling.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = ["good", "morning", "Btechgeeks"]
# Pass the given list to the random.shuffle() method that reorganizes the order of
# items in a given list.
random.shuffle(gvn_lst)
# Print the above-given list after shuffling randomly.
print("Given list After shuffling :", gvn_lst)

Output:

Given list After shuffling : ['Btechgeeks', 'morning', 'good']
Using Function

To weigh or specify the result, you can define your own function.

If the function returns the same number every time, the result will be the same every time

Approach:

  • Import random module using the import keyword.
  • Create a function say demo_function.
  • Inside the function, return 0.1
  • Give the list as static input and store it in a variable.
  • Pass the given list, above function as arguments to the random.shuffle() method in which if the function returns the same number every time, the result will be the same every time.
  • Print the above-given list after shuffling.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random

# Create a function say demo_function.


def demo_function():
    # Inside the function, return 0.1
    return 0.1


# Give the list as static input and store it in a variable.
gvn_lst = ["hello", "this", "is", "btechgeeks"]
# Pass the given list, above function as arguments to the random.shuffle() method
# in which if the function returns the same number every time, the result will be
# the same every time.

random.shuffle(gvn_lst, demo_function)
# Print the above-given list after shuffling.
print(gvn_lst)

Output:

['this', 'is', 'btechgeeks', 'hello']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list to the random.shuffle() method that reorganizes the order of items in a given list.
  • Print the above-given list after shuffling.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list to the random.shuffle() method that reorganizes the order of
# items in a given list.
random.shuffle(gvn_lst)
# Print the above-given list after shuffling randomly.
print("Given list After shuffling :", gvn_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40
Given list After shuffling : [20, 40, 30, 10]