A lottery, a lucky draw, and a card game. What do all three of these games have in common? All of them require a step to be performed. What is that step? These three games require shuffling. In various cases of programming, you will be required to shuffle a given set of items.
There is a need to shuffle a given set of items. In this article, we will learn how to shuffle a list in python. And Yes, you can do it with and without using the shuffle command.
What is a list in python?
A list in python is a data structure that provides sequential access to a collection of items. Too much technical jargon? Well, imagine that scenario that your mom is sending you out to get groceries, she will hand you some money and along with that a list.
The list will be a sequence of items that you need to buy and you can use the list to see what item to buy at any time. Similarly, in python, you can create a list that allows you to store multiple items under one name and then use that name to access all the items.
There are two methods that can be used to declare a list in python, the first method is to enclose all the items that we want to include in the list inside square braces [ ].
The other method is to use the list() function to create a list. You can pass all the items that you want to include in the list inside the list() function and it will return a list with all the items.
You can also create empty lists to which you can add elements later in your code. To do this you either use empty square braces or the list() function with no arguments. Have a look at the code below to understand this further:
#Creating a list using square braces lis1 = [15,12,48,19,63] #Creating a list using list function lis2 = list((24,18,19,47)) #Creating an empty list emptylis1 =  emptylis2 = list() print("List1: ",lis1,"\n","List2: ",lis2,"\n","EmptyLists: ",emptylis1,emptylis2)
List1: [15, 12, 48, 19, 63] List2: [24, 18, 19, 47] EmptyLists:  
Learn more about python lists here.
How to shuffle a list in Python?
There are 03 methods to shuffle a list in python, Fisher-Yates Shuffle Algorithm, the shuffle method, and the sampling method. Let us discuss all of them in detail.
01) Fisher-Yates Algorithm
The Fisher-Yates Algorithm provides a technique to shuffle the items in a list by swapping the place of an item with another item from a random index in the list. An index is a number inside the memory that can be used to access elements in a list. If we have a list,
Lis1 = [25,28,21,20]
then the process to access a number say 28 would be Lis, where 1 is the index of 28. Indexes start at 0 from the first item in a list and go in sequential order to the last item in a list. The Fisher-Yates Algorithm uses the index to shuffle the items of the list.
The algorithm goes through a list in reverse, starting from the last index, and uses a method in python, called randint(). The randint() method generates a random integer value between the start and stops points that we provide.
In the Fisher-Yates Algorithm, the start point will be 0 and the stop point will be the length of the list minus 1 for the randint() method. The Fisher-Yates Algorithm causes the original indices of the list to be lost, so we cannot go back from shuffling the list to the original list.
The python code implementation for this is shown below:
import random lis1 = [15,12,48,19,63] print("The original list is: ",lis1) for i in range(len(lis1)-1,0,-1): j = random.randint(0,i+1) #selecting a random item's index lis1[i],lis1[j] = lis1[j],lis1[i] #swapping the two items print("The shuffled list is: ",lis1)
The original list is: [15, 12, 48, 19, 63] The shuffled list is: [15, 19, 12, 63, 48]
02) Shuffle Method
Instead of implementing an algorithm by manually coding the algorithm, you can also shuffle a list in python by using its in-built library. A library is a collection of modules or pre-defined functions that you can directly call and use in your code without worrying about its implementation.
To shuffle a list, we can use the random library and the shuffle() method that is available inside it. The shuffle method takes the list that we want to shuffle as an argument and then shuffles it in place.
This means that the shuffle method does not return anything but makes the changes on the original list itself. The original list is hence lost when we use the shuffle method.
The python code for the shuffle method is given below;
import random lis1 = [15,12,48,19,63] print("The original list is: ",lis1) random.shuffle(lis1) #shuffle method print("The shuffled list is: ",lis1)
The original list is: [15, 12, 48, 19, 63] The shuffled list is: [19, 48, 12, 63, 15]
03) Sample Method
In certain situations, we might need to keep the original list intact without disturbing its items. But we also need to shuffle the list of items. In this situation, we can use the sample() method that is available in the random library of python.
The sampling method does not disturb the original list and returns a shuffled list of all the items in the list. In this method, we do not lose the original list and at the same time also obtain the shuffled list we require.
The code implementation for this is provided below:
import random lis1 = [15,12,48,19,63] print("The original list is: ",lis1) lis2 = random.sample(lis1,len(lis1)) print("The original list after shuffling: ",lis1) print("The shuffled list is: ",lis2)
The original list is: [15, 12, 48, 19, 63] The original list after shuffling: [15, 12, 48, 19, 63] The shuffled list is: [48, 63, 12, 15, 19]
Lists in python are one of the basic data structures that exist and now you know how 3 different ways to shuffle a list in python with code. You can also shuffle the list without using the shuffle command.
The Fisher-Yates Algorithm swaps two items randomly, the shuffle() method and the sample() method. The first two methods cause the original list to be lost whereas the third method preserves the original ordering of the list.