random.shuffle() method will shuffle a list in Python. It uses the
random module to reorder every item in a Python list. The shuffle can be controlled with an optional second parameter.
When you’re working with a list in Python, you may encounter a situation where you want to randomize the order of items in a list. For instance, if you’re creating a program that chooses the winner of a raffle at your store, you may want to have that program select a winner by randomly reorganizing the list of participants.
That’s where the
random.shuffle() method comes in. The
random.shuffle() method is part of the
random module in Python and is used to reorder the items in a list at random.
This tutorial will explore the
random.shuffle() method in-depth and will walk through an example of how you can use the method in your Python program.
Python Shuffle List
list is a data type in Python; lists store collections of data. Lists in Python are ordered, which means that when you store an item in a list, it will stay in that position. This is different from data types such as dictionaries, which are not ordered.
While this is an important feature of lists, you may be creating a program that needs to shuffle the order of a list. For instance, you could be creating an app that chooses which player goes first in a game. This application would need to reorganize the list of players at random and return a revised list that indicates the order of play.
random.shuffle() Python function randomly reorders items in a list.
random.shuffle() is part of the random library, which implements pseudo-random number generators in Python. This means that before we can use the
random.shuffle() method, we need to import the random library into our code. We can do so by adding the following to our code:
The syntax for the
random.shuffle() method is as follows:
import random random.shuffle(list_name, function)
random.shuffle() method takes in two parameters:
- list_name: the
stringwhose order you want to shuffle (required)
- function: the name of a function that returns a random float between 0.0 and 1.0, or another float within that range (optional)
function parameter with
random.shuffle() is uncommon. You only use it when you want to employ a custom shuffle algorithm that weighs values differently. Thus, we do not cover the
function parameter of
random.shuffle() in this tutorial.
Let’s walk through an example to illustrate the
random.shuffle() function in a Python program.
Say we are running a raffle at our store and the winner will receive a $100 gift card that he or she can spend on any product in the store. The person in second-place will receive a $50 gift card, and the person in third-place will receive a $25 gift card.
We have a list that contains the name of everyone who entered the raffle, and we want to reorganize our list in a random order to determine the winners. To do this, we’ll use the
Here’s the code we could use to reorganize the order of our list randomly so that we can identify our winners:
import random raffle_entrants = ['Thomas Crane', 'Braden Cox', 'Adie Paulson', 'Leonardo Downs', 'Lindsay Knapp', 'Carl Sanderson'] print('Entrants (ordered)') print(raffle_entrants) print('Winners (random order)') random.shuffle(raffle_entrants) print(raffle_entrants)
Our function returns the following:
Entrants (ordered) ['Thomas Crane', 'Braden Cox', 'Adie Paulson', 'Leonardo Downs', 'Lindsay Knapp', 'Carl Sanderson'] Winners (random order) ['Adie Paulson', 'Braden Cox', 'Carl Sanderson', 'Leonardo Downs', 'Thomas Crane', 'Lindsay Knapp']
Let’s discuss our code line by line so we can explore what is going on.
On the first line, we import the random library, which contains the
random.shuffle() method that we use later in our code. Then, we declare a list called
raffle_entrants, which includes the name of everyone who entered the raffle.
On the next two lines, we print out a message to the console saying,
Entrants (ordered), followed by the contents of the
raffle_entrants list. This allows us to see our list before the program shuffles it.
Then, we print out a message to the console saying,
Winners (random order), and we use
random.shuffle() to shuffle the contents of our
raffle_entrants list. Finally, we print out our
raffle_entrants list after it is shuffled using
Now we have a randomly shuffled list of names. So, according to the example above, we now know that Braden Cox won first place, Thomas Crane won second place, and Leonardo Downs won third place.
If we run our code again, we get a newly ordered Python list. Here’s what happens when we execute our code a second time:
Entrants (ordered) ['Thomas Crane', 'Braden Cox', 'Adie Paulson', 'Leonardo Downs', 'Lindsay Knapp', 'Carl Sanderson'] Winners (random order) ['Braden Cox', 'Lindsay Knapp', 'Carl Sanderson', 'Leonardo Downs', 'Thomas Crane', 'Adie Paulson']
Thus, it’s clear that
random.shuffle() will return a randomly organized list (technically, a pseudo-randomly organized list) every time we use it.
You can randomize the order of items in a list in Python using the
random.shuffle() method. In our example,
random.shuffle() helped us create a program that randomly selected raffle winners from a list of entrants.
This tutorial explored how to use the
random.shuffle() method in Python and explored an example of the method in action. Now you have the knowledge you need to use
random.shuffle() in your code like a Python pro!