Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to shuffle a list in Python

Getting Started

Python is a high-level, interpreted, interactive, and object-oriented scripting language. It's designed to be highly readable, using English keywords frequently where other languages use punctuation. Today, we will be exploring a common task in Python, which is shuffling a list. This task is particularly useful in data science, game development, and many other fields where randomization is important.

What is a List?

Before we delve into the process, let's quickly brush up on what a list is in the context of Python programming. A list is a data structure in Python that is changeable, or mutable, and can contain elements of different data types. For instance, a list can include integers, strings, and even other lists. Here is an example of a list:

fruits = ["apple", "banana", "cherry", "date"]

Shuffling a List

Shuffling a list means rearranging its elements in a random order. Imagine you have a deck of cards. When you shuffle that deck, you're randomizing the order of the cards. We do the same with a list in Python.

The random Module

To shuffle a list in Python, we need to use a built-in Python module called random. This module provides various functions that involve randomization, and one of these functions is shuffle().

Here is an example of how you might use the shuffle() function:

import random

fruits = ["apple", "banana", "cherry", "date"]
random.shuffle(fruits)

print(fruits)

When you run this code, you might get this output:

['date', 'banana', 'cherry', 'apple']

If you run the code again, you'll probably get a different output. This is because the shuffle() function randomizes the order of the list elements each time it is called.

How does random.shuffle() work?

The random.shuffle() function uses a popular algorithm known as the Fisher-Yates shuffle. The Fisher-Yates shuffle is an algorithm for generating a random permutation of a finite sequence. In plain terms, it's a way to arrange a list of items in a random order.

Let's say you have a list of three numbers: [1, 2, 3]. The Fisher-Yates shuffle starts from the last element, randomly selects one of the previous elements, and swaps it with the current element. It then moves to the next element and repeats the process until it reaches the start of the list.

What if I need a new shuffled list?

In some cases, you might want to keep the original list intact and generate a new shuffled list. To achieve this, we can use the random.sample() function.

Here's how to use random.sample() to shuffle a list:

import random

fruits = ["apple", "banana", "cherry", "date"]
shuffled_fruits = random.sample(fruits, len(fruits))

print(shuffled_fruits)

In this case, the original fruits list remains unchanged, and a new shuffled list is created.

Conclusion

Shuffling a list in Python is like making a fruit salad. You start with a list of fruits (or ingredients), and when you shuffle it, you're mixing those ingredients together in a random order. Each time you make the salad, the ingredients get mixed in a different way, but it's always delicious. That's the beauty of the random.shuffle() function!

Moreover, Python's flexibility with the random.sample() function also ensures that if you want to keep your original list of fruits intact, you can do so and still enjoy a good mix in a new bowl.

So, the next time you're faced with a list in Python that needs shuffling, whether it's a deck of cards for a game or a dataset for machine learning, remember that Python's random module has got you covered. Happy coding!