In Python, the random module offers numerous functions that can create random numbers and values. The random module functions can be used for various purposes, such as statistical analysis, developing games, etc. The Python “random.shuffle()” function of the random module allows us to shuffle the elements in a list.

This post will demonstrate an in-depth guide on the Python random.shuffle() function using multiple examples.

## Using random.shuffle() Function to Shuffle a Python List

In Python, the “random.shuffle()” function is used to shuffle the list, string, tuple, etc. The syntax of this function is demonstrated below:

random.shuffle(sequence , random)

In the above syntax, the parameter “sequence” specifies the iterable such as list, tuple, etc., that needs to be shuffled. The parameter “random” specifies the random function applied to the sequence. The “random.random()” function is used by default in this parameter.

Here are some examples of how to shuffle a list in Python:

## Example 1: Shuffle a List

The below code uses the “random.shuffle()” function to shuffle the list:

Code:

import random
list1 = [15, 20, 25, 35, 45, 98]
print('Original List: ',list1)
random.shuffle(list1)
print('After Shuffling: ', list1)
• The random module is imported at the start of the code.
• The variable named “list1” is initialized.
• The “random.shuffle()” function accepts the list as an argument and shuffles the original list.

Output:

The given list has been shuffled successfully.

## Example 2: Shuffle the Copy of the Original/Initialized List

In the following code, the original list is unchanged, and the copy of the list is shuffled:

Code:

import random
list1 = [3, 4, 5, 9, 10]
list_value = list1.copy()
random.shuffle(list_value)
print("Original list : ", list1)
print("List after shuffle", list_value)
• The “list1.copy()” function is used to create the copy of the original list and stored in the variable named “list_value”.
• The “random.shuffle()” function is used to shuffle the duplicate list without changing the original list.

Output:

The copy of the list has been shuffled, and the original list remains unchanged.

## Example 3: Shuffle Multiple Lists at Once

To shuffle multiple lists at once, we can use the “zip()” function along with the random.shuffle() function. Here’s an example:

Code:

import random
list_value_1 = [22, 15, 20, 25]
list_value_2 = ['Joseph', 'Alex', 'Lily', 'Henry']
new_list = list(zip(list_value_1, list_value_2))
random.shuffle(new_list)
list1, list2 = zip(*new_list)
print(list(list1))
print(list(list2))
• The random module is imported in the code.
• The multiple lists are initialized.
• The “zip()” function combines the lists into tuples and stores the result in “new_list”.
• The “random.shuffle()” function shuffles the list.
• Now, we unzip the shuffled tuples back into separate lists using the “zip(*new_list)” syntax.
• Lastly, we store them in the variable named “list1” and “list2”.

Output:

The above snippet shows the shuffled list.

## Example 4: Shuffle a String By Converting into List

The below code is used to shuffle a string by converting into the list:

Code:

import random
string_value = "itslinuxfoss"
print('Original String: ', string_value)
list1 = list(string_value)
random.shuffle(list1)
new_string = ''.join(list1)
print('Shuffled String: ', new_string)
• The string named “string_value” is initialized in the program.
• The “list()” function accepts the string as a parameter and converts it into a list.
• After converting a string into the list, the “random.shuffle()” function takes the list and returns the shuffled list.
• The “join()” function is used to join the shuffled list value to the empty string and stored in the variable named “new_string”.

Output:

The string has been shuffled.

## Conclusion

In Python, a built-in function named “random.shuffle()” is used to shuffle the order of elements in a list. It can be used to simultaneously shuffle a copy of the original list or multiple lists. The “random.shuffle()” is used in conjunction with “list.copy()” to shuffle the copy of the list and keep the original list. This guide presented an in-depth guide on Python’s “random.shuffle()” function.