Randomly shuffle a list in python (Best Practices)
Python provides a number of ways to randomly shuffle a list. The most common is to use the shuffle() method from the random module. This method takes a list and shuffles it in place, returning None.
Solution 1: Using random.shuffle() function of random module
To shuffle a list in python, you can use the random.shuffle() function. This function only shuffles the innermost nested list present in the given list. It doesn’t return any value.
We need to import the random module of Python to use the random.shuffle() function. To understand it better, check the below Python code example.
Syntax
random.shuffle(myList)
Code example 1: Create a list of n numbers and shuffle them
import random
my_list = list(range(0, 10))
print("Original list is: ", my_list)
random.shuffle(my_list)
print("Shuffled list is: ", my_list)
Output
Original list is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Shuffled list is: [9, 6, 5, 0, 8, 2, 3, 4, 7, 1]
In the above code example, we have created a list of n numbers using range() function and then shuffled it.
Code example 2:
import random
numbers = [10, 20, 30, 40, 50, 60]
print('Original List: ', numbers)
random.shuffle(numbers)
print('Shuffled list: ', numbers)
Output
Original List: [10, 20, 30, 40, 50, 60]
Shuffled list: [40, 30, 60, 50, 20, 10]
The above code example imports the random module and then creates a list of numbers. It shuffles the list of numbers using the shuffle method from the random module and then prints the shuffled list.
import random
def shuffle_list(my_list):
random.shuffle(my_list)
return my_list
print(shuffle_list(['John', 'Mark', 'Carl', 'Henry']))
print(shuffle_list([1, 2, 3, 4, 5, 6, 7, 8]))
['Carl', 'Mark', 'John', 'Henry']
[4, 2, 1, 6, 5, 8, 3, 7]
This is a Python code example of how to shuffle a list. The shuffle_list() function takes a list as an argument and shuffles it using the random module. Then it returns the shuffled list.
Solution 2: Using random.sample() function
If you need to shuffle a list in python, you can use the random.sample() function. This function will take a list as an input and randomly shuffle it. You can also specify the number of items you want to shuffle.
Syntax
random.sample(myList, len(myList))
Code example
import random
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
result = random.sample(numbers, len(numbers))
print('Original list is: ', numbers)
print('Shuffled list is: ', result)
Output
Original list is: [1, 2, 3, 4, 5, 6, 7, 8]
Shuffled list is: [7, 2, 1, 6, 8, 4, 5, 3]
This code is an example of how to shuffle a list in Python. The result will be a new list with the same elements as the original list, but in a random order.
Solution 3: Using numpy random.shuffle() function
If you have a list of items in Python, you can use the NumPy random.shuffle() function to randomly shuffle the items.
The shuffle() function takes a list as a parameter and shuffles the items in the list in place. This means that the original list is changed and the shuffled list is returned.
import numpy as np
# create a list
numbers = np.arange(10)
print('Original list: ', numbers)
np.random.shuffle(my_list)
print('Shuffled list is: ', my_list)
Output
Original list: [0 1 2 3 4 5 6 7 8 9]
Shuffled list is: [6, 8, 4, 7, 5, 9, 2, 1, 3, 0]
The example above imports the NumPy package and creates a list of numbers from 0 to 9. It then shuffles the list using the random.shuffle() function.
- Create a flat list from list of lists in Python
- Check if a list exists in a list of lists Python
- Convert a List of Strings to List of Integers in Python
- Python collection - List in python
- [Python] Using comprehension expression to convert list of dictionaries to nested dictionary
- [Python] Create a list of objects using class names
- [Python] Range - standard function to create a list of numbers