0
0
PythonProgramBeginner · 2 min read

Python Program to Shuffle a List Using random.shuffle()

Use random.shuffle(your_list) to shuffle a list in Python in place, which rearranges the elements randomly.
📋

Examples

Input[1, 2, 3]
Output[3, 1, 2]
Input["apple", "banana", "cherry"]
Output["banana", "cherry", "apple"]
Input[]
Output[]
🧠

How to Think About It

To shuffle a list, think of mixing cards in a deck so their order changes randomly. We want to rearrange the list elements without losing any, so each element moves to a new random position. Python's random.shuffle() does this by swapping elements randomly inside the list.
📐

Algorithm

1
Import the random module.
2
Create or get the list you want to shuffle.
3
Call the shuffle function from the random module on the list.
4
The list is rearranged randomly in place.
5
Print or use the shuffled list.
💻

Code

python
import random

my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list)
Output
[3, 1, 5, 2, 4]
🔍

Dry Run

Let's trace the list [1, 2, 3, 4, 5] through the shuffle code

1

Original list

my_list = [1, 2, 3, 4, 5]

2

Call random.shuffle

random.shuffle(my_list) rearranges elements randomly

3

Shuffled list

my_list might become [3, 1, 5, 2, 4]

4

Print result

print(my_list) outputs the shuffled list

StepList State
Initial[1, 2, 3, 4, 5]
After shuffle[3, 1, 5, 2, 4]
💡

Why This Works

Step 1: Import random module

The random module provides functions to generate random numbers and shuffle lists.

Step 2: Use random.shuffle()

random.shuffle() rearranges the list elements in place, meaning it changes the original list order randomly.

Step 3: Print the shuffled list

Printing the list shows the new random order after shuffling.

🔄

Alternative Approaches

Using random.sample() to create a new shuffled list
python
import random

my_list = [1, 2, 3, 4, 5]
shuffled_list = random.sample(my_list, len(my_list))
print(shuffled_list)
This creates a new shuffled list without changing the original list, but uses extra memory.
Manual shuffle using Fisher-Yates algorithm
python
import random

my_list = [1, 2, 3, 4, 5]
for i in range(len(my_list)-1, 0, -1):
    j = random.randint(0, i)
    my_list[i], my_list[j] = my_list[j], my_list[i]
print(my_list)
This manually implements the shuffle logic, useful for learning but more code than random.shuffle().

Complexity: O(n) time, O(1) space

Time Complexity

random.shuffle() runs in O(n) time because it swaps elements in the list once each, where n is the list length.

Space Complexity

It uses O(1) extra space since it shuffles the list in place without creating a new list.

Which Approach is Fastest?

random.shuffle() is fastest and most memory efficient compared to creating a new shuffled list with random.sample() which uses O(n) extra space.

ApproachTimeSpaceBest For
random.shuffle()O(n)O(1)In-place shuffling, memory efficient
random.sample()O(n)O(n)Creating a new shuffled list without changing original
Manual Fisher-YatesO(n)O(1)Learning shuffle algorithm, custom control
💡
Always import the random module before using shuffle to avoid errors.
⚠️
Beginners often forget that random.shuffle() changes the list in place and returns None, so assigning its result to a variable causes errors.