How to Use random.shuffle in Python

What you’ll build or solve

You’ll shuffle a list in Python with random.shuffle(), confirm the list changed, and avoid mistakes like expecting a returned value.

When this approach works best

This approach works best when you need to:

  • Randomize items in place, like shuffling a deck of cards or quiz questions.
  • Shuffle a list before picking the first N items as a quick sample.
  • Create a different order each run for demos or small games.

Avoid this approach when:

  • You need a shuffled copy while keeping the original list unchanged. Use a shuffled copy pattern instead.
  • You are shuffling for security, like passwords or tokens. Use the secrets module for security-related randomness.

Prerequisites

  • Python 3 installed
  • You know what a list is
  • You can run a short Python script

Step-by-step instructions

1) Shuffle a list in place

random.shuffle() changes the list you pass in.

import random

cards = ["A", "K", "Q", "J"]
random.shuffle(cards)
print(cards)

What to look for:

The list order changes, and shuffle() does not return a new list.


2) Keep the original list by shuffling a copy

Copy the list first, then shuffle the copy.

import random

original = ["Ana", "Bo", "Chen", "Dina"]
shuffled = original.copy()

random.shuffle(shuffled)

print("original:", original)
print("shuffled:", shuffled)

3) Shuffle with repeatable results using a seed

Set a seed when you want the same shuffle every run, such as in tests.

import random

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

random.seed(123)
random.shuffle(items)

print(items)

What to look for:

The same seed plus the same sequence of random calls produces the same order.


4) Get a shuffled copy with random.sample()

Use this when you want a new shuffled list and do not want to mutate the original.

import random

names = ["Ana", "Bo", "Chen", "Dina"]
shuffled = random.sample(names, k=len(names))

print("original:", names)
print("shuffled:", shuffled)

Examples you can copy

1) Shuffle quiz questions before showing them

import random

questions = [
    "What does len() return?",
    "What is a list index?",
    "What does import do?",
    "What is a function?",
]

random.shuffle(questions)

for q in questions:
    print(q)

2) Shuffle a deck and draw the top 5 cards

import random

deck = ["A♠", "K♠", "Q♠", "J♠", "10♠", "9♠", "8♠"]
random.shuffle(deck)

hand = deck[:5]
print(hand)

3) Shuffle pairs while keeping items together

import random

pairs = [("Ana", "blue"), ("Bo", "green"), ("Chen", "red")]
random.shuffle(pairs)

for name, color in pairs:
    print(name, color)

Common mistakes and how to fix them

Mistake 1: Expecting shuffle() to return a list

You might write:

import random

nums = [1, 2, 3]
shuffled = random.shuffle(nums)
print(shuffled)

Why it breaks:

random.shuffle() returns None, so shuffled becomes None.

Correct approach:

import random

nums = [1, 2, 3]
random.shuffle(nums)
print(nums)

Mistake 2: Passing a tuple or string to shuffle()

You might write:

import random

text = "abcd"
random.shuffle(text)

Why it breaks:

shuffle() needs a mutable sequence. Strings and tuples cannot be shuffled in place.

Correct approach:

import random

text = "abcd"
chars = list(text)

random.shuffle(chars)
print("".join(chars))

Mistake 3: Accidentally changing a list you wanted to keep

You might write:

import random

names = ["Ana", "Bo", "Chen"]
random.shuffle(names)
print(names)

Why it breaks:

shuffle() changes the list in place, so you lose the original order.

Correct approach:

import random

names = ["Ana", "Bo", "Chen"]
shuffled = names.copy()

random.shuffle(shuffled)
print("original:", names)
print("shuffled:", shuffled)

Troubleshooting

If you see NameError: name 'random' is not defined, add import random at the top of your file.

If you see TypeError when calling shuffle(), confirm you passed a list, not a tuple or string.

If your results look the same between runs, check if you called random.seed(...) earlier in your program.

If you need a shuffled copy without changing the original list, copy the list before shuffling or use random.sample().


Quick recap

  • Use random.shuffle(my_list) to shuffle a list in place.
  • shuffle() returns None, so check the list itself after shuffling.
  • Keep the original by shuffling a copy with my_list.copy().
  • Use random.seed(...) when you need repeatable shuffles.
  • Use random.sample(my_list, k=len(my_list)) when you need a shuffled copy.