How to shuffle list elements in Python? A list is an ordered sequence of elements, and the `shuffle()` function from the built-in random module can be used to shuffle the order of the elements in the Python list. The `shuffle()` function takes a sequence, such as a list, and modifies it in place by shuffling the order of its elements randomly.

You can shuffle a list in Python using many ways, for example, by using the `random.shuffle()`, `random.sample()`, `Fisher-Yates shuffle Algorithm`, `itertools.permutations()`, `reduce() & numpy`, and `random.randint() & pop()` functions. In this article, I will explain how to shuffle a list by using all these methods with examples.

## 1. Quick Examples of Shuffle a List

If you are in a hurry, below are some quick examples of how to shuffle a list in Python.

``````
# Quick examples of shuffle a list

# Example 1: Shuffle list
# Using random.shuffle() function
random.shuffle(mylist)

# Example 2: Shuffle list
# Using random.sample() function
result = random.sample(mylist, len(mylist))

# Example 3: Using Fisher–Yates shuffle algorithm
# to shuffle a list
for x in range(len(mylist)-1, 0, -1):
y = random.randint(0, x + 1)
mylist[x], mylist[y] = mylist[y], mylist[x]

# Example 4: Using itertools.permutations() function
permutations = list(itertools.permutations(mylist))
result = random.choice(permutations)

# Example 5: Shuffle a list
# Using random.randint() and pop() function
n = len(mylist)
for i in range(n):
j = random.randint(0, n-1)
element=mylist.pop(j)
mylist.append(element)
``````

## 2. Python Shuffle List using random.shuffle() Function

The random.shuffle() is a commonly used and recommended method to shuffle a list in Python. This shuffle method actually shuffles the element in place meaning it modifies the original list, hence, the ordering of the original elements in the List is lost. However, this may not be a problem if the original ordering is not important for the specific use case.

### 2.1 Syntax of random.shuffle()

Following is the syntax of the list random.shuffle().

``````
# Syntax of random.shuffle()
random.shuffle(sequence, random)
``````

### 2.2 Parameters of random.shuffle()

• `sequence` – The sequence can be a list, tuple, string, etc.
• `random` (optional)- Use function, by default it uses random(). Since version 3.9 it’s been deprecated and removed in version 3.11.

### 2.3 Shuffle List Example

Following is an example of how to perform a list shuffle. Here, we take a list as input to the shuffle() and it returns shuffle all elements in the input list.

Note that the shuffle method only works with lists, not with other sequence types like tuples or strings as these are immutable.

``````
# Import random
import random

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Shuffle list
# Using random.shuffle() function
random.shuffle(mylist)
print("Shuffle list: ", mylist)
``````

Yields below output.

## 3. Using sample() to Shuffle List

Python `random.sample()` function is also available in the random module, which will return the random items of a specified length from the iterable objects like liststringtupleset, etc. The random elements from the iterable objects are returned in a list.

### 3.1 Syntax of random.sample()

Following is the syntax of random.sample() function.

``````
# Syntax of random.sample()
random.sample(sequence/iterable, counts, k)
``````

### 3.2 Parameters of random.sample()

It takes three parameters.

• `sequence:` Is the sequence/iterable in which random numbers are generated from this sequence/iterable.
• `counts:` It is an optional parameter that will represent the frequency of each element in the specified sequence/iterable. We need to pass the frequencies through a list.
• `k :` It is the integer value that will specify the length of the sample. Its length must be less than or equal to the passed iterable object, otherwise `ValueError `will be raised.

### 3.3 Return Value

It returns a randomly selected subset of numbers from a given sequence/iterable object.

### 3.4 Shuffle a List using sample() Example

First import the random module, which provides various functions related to random numbers, and define our original list `mylist` containing six elements, finally call the random.sample() function and pass in our original list `mylist` as the first argument and the length of the list as the second argument using the len() function. This returns a new list that contains the elements of the original list in a randomly shuffled order.

``````
# Import random
import random

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Shuffle list
# Using random.sample() function
result = random.sample(mylist, len(mylist))
print("Shuffle list: ", result)
``````

Yields below output.

## 4. Using Fisher-Yates Shuffle Algorithm

The `Fisher-Yates` Shuffle Algorithm is also known as the `Knuth shuffle` algorithm which is widely used to shuffle a sequence of items. This algorithm iterates over the elements of the list in reverse order, starting from the last element and ending at the second element.

At each iteration, it generates a random index within the range of the unshuffled elements and swaps the element at the current index with the element at the randomly generated index. This process is repeated until all the elements in the list have been shuffled.

``````
import random

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Using Fisher–Yates shuffle Algorithm
# to shuffle a list
for x in range(len(mylist)-1, 0, -1):
y = random.randint(0, x + 1)
mylist[x], mylist[y] = mylist[y], mylist[x]
print("Shuffle list: ", mylist)
``````

Yields below output.

``````
# Output:
Original list:  [5, 10, 20, 30, 40, 50]
Shuffle list:  [40, 20, 10, 30, 50, 5]
``````

## 5. Shuffle a List Using itertools.permutations() Function

The `itertools.permutations()` function generates all possible permutations of a given sequence, but it does not shuffle the elements of the sequence randomly. If you want to shuffle a list randomly using `itertools.permutations()`, you can generate all possible permutations and then select one permutation randomly.

For example, define a list `[5, 10, 20, 30, 40, 50]` that you want to shuffle randomly, then use `itertools.permutations()` to generate all possible permutations of the list and store them in permutations. You then convert permutations to a list `permutations_list` so that you can access its elements randomly. Use the` random.choice()` function to select a random permutation from the `result`.

``````
# Import modules
import random
import itertools

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Using itertools.permutations() function
permutations = list(itertools.permutations(mylist))
result = random.choice(permutations)
print("Shuffle list: ", result)

# Output:
# Original list:  [5, 10, 20, 30, 40, 50]
# Shuffle list:  (30, 40, 10, 20, 5, 50)
``````

## 6. Using random.randint() and pop() Function

You can also shuffle elements using random.randint() and pop() function. First, you start by getting the length of the list `mylist` and storing it in the variable `n`. Then, you loop through the range `0` to `n-1` using a for loop. This loop iterates `n` times.

For each iteration, get the random integer from the list using the randint(), pop that element at the index using pop(), and then append element to the list using append().

``````
# Import
import random

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: ", mylist)

# Shuffle a list
# Using random.randint() and pop() function
n = len(mylist)
for i in range(n):
j = random.randint(0, n-1)
element=mylist.pop(j)
mylist.append(element)
print("Shuffled List: ",mylist)
``````

Yields below output.

``````
# Output:
Original list:  [5, 10, 20, 30, 40, 50]
Shuffled List:  [20, 30, 50, 40, 10, 5]
``````

## 7. Shuffle a List Using reduce() and NumPy

You can also shuffle the elements of a list using the reduce() function from the `functools` module and the `np.random.permutation()` function from the NumPy library.

``````
# Import
import numpy as np
from functools import reduce

# Initialize list
mylist = [5, 10, 20, 30, 40, 50]
print("Original list: " + str(mylist))

# Using reduce() and numpy
# To shuffle a list
result = reduce(lambda acc, _: np.random.permutation(acc),
range(len(mylist)), np.array(mylist))
print("Shuffled list: " + str(result.tolist()))
``````

Yields below output.

``````
# Output
Original list: [5, 10, 20, 30, 40, 50]
Shuffled list: [20, 5, 30, 50, 40, 10]
``````

## Conclusion

In this article, you have learned Python list shuffle can be done by using `random.shuffle()`, `random.sample()`, `Fisher-Yates shuffle Algorithm`, `itertools.permutations()`, `reduce() & numpy`, and, `random.randint() & pop()`. Based on your need, you can use any approach explained in this article.

Happy Learning !!