Shuffle a List in Python
To shuffle a list in Python, you can use the shuffle()
function from the random
module, which randomly rearranges the elements of the list in place. You can also use alternative methods like sample()
or Fisher-Yates shuffle for different shuffling needs.
Examples
1. Using random.shuffle()
to Shuffle a List
The shuffle()
function from the random
module randomly rearranges the elements of a list. This function modifies the original list in place.
import random
# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]
# Shuffling the list
random.shuffle(numbers)
# Printing the shuffled list
print("Shuffled List:", numbers)
Explanation:
- The
random
module is imported to access theshuffle()
function. - The
numbers
list contains integers from 1 to 5. - The
shuffle()
function is called on thenumbers
list, which rearranges its elements randomly. - The shuffled list is printed as output.
Output (Example):
Shuffled List: [3, 1, 5, 2, 4]
Each time the program runs, the order of elements in the list will be different.

2. Using random.sample()
to Shuffle Without Modifying the Original List
The sample()
function from the random
module returns a new shuffled list without modifying the original list.
import random
# Creating a list of colors
colors = ["red", "blue", "green", "yellow", "purple"]
# Creating a shuffled version of the list
shuffled_colors = random.sample(colors, len(colors))
# Printing the shuffled list
print("Shuffled List:", shuffled_colors)
# Printing the original list
print("Original List:", colors)
Explanation:
- The
colors
list contains five string elements. - The
random.sample()
function takes two parameters: the list to shuffle and the number of elements to return. - The length of the list is passed as the second argument, ensuring all elements are included in the shuffled version.
- The function returns a new list (
shuffled_colors
) without altering the originalcolors
list.
Output (Example):
Shuffled List: ['yellow', 'blue', 'purple', 'green', 'red']
Original List: ['red', 'blue', 'green', 'yellow', 'purple']
Here, the sample()
method produces a shuffled version of the list while keeping the original list unchanged.

3. Using Fisher-Yates Algorithm (Manual Shuffle)
The Fisher-Yates algorithm provides an alternative way to shuffle a list using index swapping.
import random
# Creating a list of names
names = ["Alice", "Bob", "Charlie", "David", "Eve"]
# Implementing Fisher-Yates Shuffle
for i in range(len(names) - 1, 0, -1):
j = random.randint(0, i)
names[i], names[j] = names[j], names[i] # Swap elements
# Printing the shuffled list
print("Shuffled List:", names)
Explanation:
- The
names
list contains five names. - The Fisher-Yates algorithm iterates from the last index to the first.
- For each index
i
, a random indexj
(between 0 andi
) is chosen. - The elements at
i
andj
are swapped to shuffle the list.
Output (Example):
Shuffled List: ['Eve', 'Charlie', 'Bob', 'Alice', 'David']
This method ensures an unbiased shuffle by randomly swapping elements iteratively.

Conclusion
Python provides multiple ways to shuffle a list, each with its use case:
random.shuffle()
: Shuffles the list in place, modifying the original list.random.sample()
: Creates a shuffled version of the list without changing the original.- Fisher-Yates Algorithm: Implements manual shuffling using index swapping.
If you need an in-place shuffle, use shuffle()
. If you want a new shuffled list while keeping the original intact, use sample()
. The Fisher-Yates algorithm is useful when implementing custom shuffle logic.