Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to randomize a list in Python

A World Without Randomness

Imagine a world where everything is predictable, where the outcome of all events is predetermined. Sounds quite boring, right? In programming, the same principle applies. To make our programs more dynamic and versatile, we often need to introduce some level of randomness. Today, we will discuss one such scenario: randomizing a list in Python.

What is a List in Python?

Before we dive into how to randomize a list, let's first understand what a list is. In Python, a list is a built-in data type that can store multiple items in a single variable. It's like a backpack. You can put in various items (like integers, strings, even other lists) and carry them around together.

For example, a list of your favorite fruits might look like this:

favorite_fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

Why Would We Need to Randomize a List?

Let's say you're hosting a game show and have a list of questions. If you ask these questions in the same order every time, it wouldn't take long for contestants to catch on and anticipate the sequence. To keep things fair and interesting, you might want to ask these questions in a random order every time. This is just one scenario where list randomization can come in handy.

The Shuffle Function: A Pythonic Solution

Python, being an all-purpose language, has a built-in function to randomize lists: the shuffle() function from the random module. Think of it as shaking the backpack of fruits and then taking them out one by one without looking.

Let's see it in action:

import random

favorite_fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']


When you run this code, you'll see the favorite_fruits list in a different order each time.

How Does Shuffle Work Under the Hood?

The shuffle function uses what's called the Fisher-Yates algorithm, also known as the Knuth shuffle. This algorithm works by going through the list from the last item to the first and for each item, swapping it with an item at a random index less than or equal to its current one.

Think of it like playing a game of musical chairs with the fruits in your backpack. The music starts (the algorithm starts), and when the music stops (the algorithm selects a random index), the fruit in hand swaps its place with the one in that chair (index).

Randomizing a List Without Using Shuffle

What if, for some reason, we don't want to use the shuffle function? Maybe we want to keep the original list intact or need to randomize within certain conditions. Python has another function for that: random.sample().

Here's how to do it:

import random

favorite_fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']
randomized_fruits = random.sample(favorite_fruits, len(favorite_fruits))


In this case, random.sample() returns a new list that's a randomly shuffled version of the original list, without modifying the original list.

A Note on Randomness and Reproducibility

When working with random functions in Python, it's important to note that they aren't truly random in the purest sense, but rather pseudorandom. This means they use algorithms to generate sequences that seem random, but are actually reproducible if you know the initial "seed" value.

For testing and debugging, you might want the "random" actions in your code to be reproducible. Python's random module lets you set the seed value with random.seed(). If you set the same seed before generating a random list, you'll get the same "random" list every time.

import random

favorite_fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']


This will always print the same "random" order of the list when run.

Wrapping Up

Randomizing lists is a common task in Python programming. By shaking up the order of items in a list, we can make our programs more dynamic and unpredictable. Whether you're hosting a game show, conducting scientific research, or just trying to surprise your friends with a random selection from your favorite fruits, the techniques we've discussed today will help you add a dash of unpredictability to your Python programs.

However, remember that with great power comes great responsibility. Use randomness wisely, and don't forget that Python's pseudorandom functions are not suitable for all applications, especially those requiring high levels of security or true randomness.

So go ahead, shake up your lists, and let the Python randomness add some unpredictability to your code!