Shuffle in Python

Shuffling refers to the rearranging of elements in a random order which means no specific sequence is considered while arranging the elements.

In this tutorial, we will learn how we can shuffle the elements of a list using Python.

The different approaches that we will use to shuffle the elements are as follows-

  1. Using Fisher-Yates shuffle algorithm
  2. Using shuffle()
  3. Using sample()
  4. Random selection of elements and then appending them in a list

We will discuss each method in detail.

So, let's begin with the first one,

Using Fisher-Yates Shuffle Algorithm

Output:

The initialized list is :  [11, 20, 19, 43, 22, 10]
The shuffled list is :  [11, 43, 20, 19, 10, 22]

Explanation

Let's understand what we have done in the above program.

  1. In the first step, we have imported the random module.
  2. After this, we have an initialized list that contains different numeric values.
  3. In the next step, we used the for loop and then randomly selected an element and then swapped it with the element at random index.
  4. Finally, we have displayed the shuffled list in the output.

Using shuffle()

In the second method, we will see how shuffle() can be used to shuffle the elements of our list.

Consider the program given below-

Output:

The initialized list is :  [11, 20, 19, 43, 22, 10]
The shuffled list is :  [22, 10, 20, 11, 19, 43]

Explanation

Let's understand what we have done in the above program,

  1. In the first step, we have imported the random module.
  2. After this, we have an initialized list that contains different numeric values.
  3. In the next step, we used the shuffle() and passed 'list_values1' as a parameter.
  4. Finally, we have displayed the shuffled list in the output.

Using random.sample()

In the third approach, we will use random.sample() to do the same.

The following program illustrates how it can be done-

Output:

The initialized list is :  [11, 20, 19, 43, 22, 10]
The shuffled list is :  [43, 20, 19, 11, 10, 22]

Explanation

It's time to understand the above program-

  1. In the first step, we have imported the random module.
  2. After this, we have an initialized list that contains different numeric values.
  3. In the next step, we used the sample() and passed 'list_values1' and the length of the list as its parameters.
  4. Finally, we have displayed the shuffled list in the output.

Finally, it's time to discuss the last approach, it's indeed an interesting one, let's see how….

Random Selection of Elements and then Appending them in a list

Output:

The initialized list is :  [11, 20, 19, 43, 22, 10]
The shuffled list is :  [19, 22, 20, 43, 10, 11]

Explanation

Now, let's see the explanation of this code,

  1. In the first step, we have imported the random module.
  2. After this, we have an initialized list that contains different numeric values and then calculated the length of the list.
  3. In the next step, we used the sample() and passed 'list_values1' and the length of the list as its parameters.
  4. Now we have used for loop that randomly selects an element, removes it, and then appends it to the list.
  5. Finally, we have displayed the shuffled list in the output.

Conclusion

In this tutorial, we learned the different methods of shuffling the elements of a list in Python.






Latest Courses