The Python random.shuffle() method changes the order of the items in a list at random. Combined with indexing, this method is useful for selecting a random item from a list. The random.shuffle() method accepts one argument: the list you want to change.
You may encounter a situation where you want to randomize the order of items in a list. For instance, say 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. In this tutorial, we’ll discuss how to use the random.shuffle() method.
Python Shuffle List
The random.shuffle() Python function randomly reorders items in a list. This method is useful for applications where you want to retrieve an item from a list at random. The random.shuffle() method modifies an original list. This method does not create a new list.
A list stores collections of data. Python lists are ordered, which means that when you store an item in a list, it will stay in that position. While this is an important feature of lists, you may be creating a program that needs to shuffle the order of 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:
import random random.shuffle(list_name, function)
The random.shuffle() method takes in two parameters:
- list_name: the list, tuple, or string whose 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).
Using the 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.
You can use the random.shuffle() method on a string or a tuple. In any case, random.shuffle() modifies the original data. It does not create a new value, like a new string or a new tuple.
Python random.shuffle(): Example
Say we are running a raffle at our store. 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. Our goal is to reorganize our list in a random order to determine the winners. To do this, we’ll use the random.shuffle() method.
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']
On the first line, we import the random library using a Python import statement. This library contains the random.shuffle() method that we use later in our code. Then, we declare a Python variable 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.
We print out a message to the console saying, “Winners (random order)”. Our code uses the random.shuffle() method to shuffle the contents of our raffle_entrants list. Finally, we print out our raffle_entrants list after it is shuffled using random.shuffle().
Now we have a randomly shuffled list of names. 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.
Verifying Our Code
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.
Do you want to learn more about Python programming? Read our How to Learn Python guide. In this guide, you’ll find top tips on how to learn Python. The guide also includes a list of learning resources and courses that will help you advance your Python knowledge.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.