In this tutorial, I will explain how to shuffle a list in Python. As a Python developer working on a project, I came across a scenario where I needed to shuffle the list elements. After exploring this topic, I found a few effective methods to achieve this task. I will share my findings along with suitable examples and screenshots.
Shuffle a List in Python
To shuffle a list in Python, you can use the random.shuffle() function from the built-in random module. For example, if you have a list of names like names = ["Alice", "Bob", "Charlie", "David"] you can shuffle it by calling random.shuffle(names) which will randomly rearrange the elements in the list in place.
Read How to Convert a Dictionary to a List in Python?
Use the random Module
Python provides a built-in module called random that includes functions to generate random numbers and shuffle sequences. The primary function we will use for shuffling lists is random.shuffle().
Example 1: Shuffle a List of Names
Let’s say you have a list of participants for a local community event in New York City, and you want to shuffle their names for a random drawing.
import random
# List of participants
participants = ["John Smith", "Emily Johnson", "Michael Brown", "Jessica Davis", "David Wilson"]
# Shuffle the list
random.shuffle(participants)
print("Shuffled participants:", participants)Output:
Shuffled participants: ['Michael Brown', 'David Wilson', 'Jessica Davis', 'John Smith', 'Emily Johnson']You can see the output in the screenshot below.

In this example, the random.shuffle() function rearranges the names in the participants list in place. Each time you run the code, you will get a different order of names.
Check out How to Get the Index of an Element in a List in Python?
1. Use random.shuffle() for Shuffling
The Python random.shuffle() function modifies the original list. This means that if you need to keep the original order for any reason, you should make a copy of the list before shuffling:
# Copy the original list
original_participants = participants.copy()
# Shuffle the copy
random.shuffle(participants)
print("Original participants:", original_participants)
print("Shuffled participants:", participants)Output:
Original participants: ['John Smith', 'Emily Johnson', 'Michael Brown', 'Jessica Davis', 'David Wilson']
Shuffled participants: ['Jessica Davis', 'Emily Johnson', 'John Smith', 'Michael Brown', 'David Wilson']You can see the output in the screenshot below.

Read How to Sort a List in Python Without Using the sort() Function?
2. Use random.sample() for Shuffling
While random.shuffle() is great for shuffling in place. You can also use random.sample() to create a new shuffled list without altering the original. This method is particularly useful when you want to retain the original list for further use.
Example 2: Randomly Sampling a List of Cities
Suppose you have a list of major cities in the USA, and you want to create a random itinerary for a road trip.
import random
# List of cities
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
# Create a shuffled copy of the list
shuffled_cities = random.sample(cities, len(cities))
print("Random road trip itinerary:", shuffled_cities)Output:
Random road trip itinerary: ['Phoenix', 'New York', 'Houston', 'Los Angeles', 'Chicago']You can see the output in the screenshot below.

In this example, random.sample(cities, len(cities)) returns a new list containing all the elements of cities but in a shuffled order. The original cities list remains unchanged.
Check out How to Merge Lists Without Duplicates in Python?
Custom Shuffle Function
If you need more control over the shuffling process, you can implement your shuffle function. This might be useful if you want to use a specific algorithm or customize the shuffling criteria.
Example 3: Custom Shuffle Implementation
Here’s a simple implementation of the Fisher-Yates shuffle algorithm, which is an efficient way to shuffle a list:
def custom_shuffle(lst):
for i in range(len(lst) - 1, 0, -1):
j = random.randint(0, i)
lst[i], lst[j] = lst[j], lst[i] # Swap elements
# List of famous American landmarks
landmarks = ["Statue of Liberty", "Grand Canyon", "Yellowstone National Park", "Mount Rushmore", "Disneyland"]
# Shuffle using the custom function
custom_shuffle(landmarks)
print("Shuffled landmarks:", landmarks)This function iterates through the list and swaps each element with a randomly chosen element that comes before it (including itself), ensuring a random order.
Read How to Convert String to List in Python?
Shuffle Lists of Objects
Sometimes, you may need to shuffle a list of objects, such as a list of student records or product listings. The same methods apply, but let’s look at an example with a list of dictionaries.
Example 4: Shuffle a List of Student Records
Imagine you have a list of student records from a high school in California, and you want to shuffle them for a random selection process.
students = [
{"name": "Alice Johnson", "grade": 10},
{"name": "Brian Adams", "grade": 11},
{"name": "Catherine Lee", "grade": 12},
{"name": "Daniel Kim", "grade": 10},
{"name": "Eva Martinez", "grade": 11}
]
# Shuffle the list of student records
random.shuffle(students)
print("Shuffled student records:", students)This will shuffle the entire list of dictionaries, providing a random order for further processing, such as selecting students for a special program.
Check out Convert String to List in Python Without Using Split
Best Practices for Shuffling Lists
- Understand the Functionality: Always know whether the function modifies the original list or creates a new one. Use
random.sample()if you need to keep the original list intact. - Test Your Shuffle: If you’re using shuffling in a critical application (like a game), make sure to test the randomness of your shuffle to ensure fairness.
- Use Seed for Reproducibility: If you need the same random order for testing purposes, you can set a seed using
random.seed(). This allows you to reproduce the same shuffle.
random.seed(42) # Set a seed
random.shuffle(participants)Read How to Iterate Through a List in Python?
Conclusion
In this article, I explained how to shuffle a list in Python. I discussed two methods using the random module, such as using random.shuffle() function and using the random.sample() function. I also covered how to customize the shuffle function , shuffle lists of objects, and some best practices for shuffling lists.
You may also read:
- How to Write a List to a File in Python?
- How to Select Items from a List in Python?
- How to Add Tuples to Lists in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.