Python Shuffle List

In Python, the idea of shuffle derives from the shuffling deck of playing cards. Shuffling is a technique used to shuffle a deck of cards and randomly pick one in card games. In Python, shuffling is used to get a completely filtered randomized list. We use the built-in function “shuffle()” to shuffle a Python list like we shuffle cards. In shuffle(), the list is fed as an argument. The function shuffles the list, and then, it updates the list. The function does not create another list to transfer shuffled elements into it. The shuffle() function takes an order and rearranges the order. Today, we will discuss how to use Python code to shuffle elements in a list, thereby reorganizing Python list items. For this purpose, we use several methods for its implementation.

Note: The operating system in this Python shuffle list article is Windows 10. Also, Spyder is used for the execution of the sample programs.

Example 1:

In this example, we use the Shuffle() function to shuffle the Python list of fruits_name. At first, we are using a random module because the shuffle function is present in it. Then, we create and initialize a list of fruits. You can create and add any values to the list. Then, we use the Shuffle() function to mix up the list. To shuffle the list and implement the code, we use Spyder Compiler. Simply launch it from the Windows search bar, then create a new file for the program implementation. You can check out this program here:

import random
fruits_name = ["Banana", Apple", “Peach", "Orange"]

Once we successfully write our Python shuffle list program, save your source code file from the File menu just like we did in the following screenshot:

Now, run your code file by hitting the F5 shortcut key and view how the shuffle() function shuffles the fruit list in Python code. The output is shown here:

Example 2:

In this example, we use random.sample() function. The function randomly chooses the elements from the list and creates a new filtered shuffled list. First, it takes the list argument, which we want to shuffle. Then, it takes another argument which refers to the length of the list or the number of elements we want to pick. We use the .sample() function to shuffle a Python list. We do this in the same source code file. You can either create a new source code file one make changes to the previous one. We use the same code file, “PythonShuffleList”. Let’s check how we have managed to shuffle our Python list.

Initially, we create or initialize our list and specify its name to the Welcome_list. We can then create a new variable, “shuffled”, which takes a random.sample() function. We fed the list and the size of our list into the random.sample() function. With the help of the len() function, we can keep this process dynamic, as the list size may change. You can check out this program here:

import random
welcome_list = ['welcome', 'to', 'the', 'where', 'world', 'of', 'Python',]
shuffled = random.shuffle(welcome_list , len (Welcome_list)

Save and run your code file, and check the shuffled output on the console screen:

Example 3:

In this example, we will discuss how to shuffle a deck of cards using the shuffle() function. We implement this in Spyder IDE and use the same code file. To create a card list, we first specify the product() function. The function executes the multiplication of the two orders. Our deck is organized, so we randomized it using the shuffle() function. You can check out this program here:

import itertools, random
deck_cards= list(itertools.product (range(1,14),['Jack', 'Heart', 'Queen', 'Club']))
print ("Your cards:")
for i in range (5):
print(deck_cards [i][0], "of", deck_cards [i][1])

You will get a different result every time you execute this code. Here, we run the code two times, and the output is shown below:


In this article, we have discussed the best methods to Shuffle list in Python. There are various methods to shuffle lists in Python. However, we have discussed the shuffle() and random.sample() methods in our illustration. We have used these methods because they are very easy to use and comprehensible. We hope you found this article helpful, and please check out our other articles.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content