random.shuffle(list) to randomize the order of the elements in the list after importing the random module with
import random. This shuffles the list in place. If you need to create a new list with shuffled elements and leave the original one unchanged, use slicing
list[:] to copy the list and call the shuffle function on the copied list.
Problem: You’ve got a Python list and you want to randomly reorder all elements?
Solution: No problem, use the shuffle function in Python’s
Example: Here’s some code to show you how to do this:
import random a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(a) print(a) # [6, 8, 5, 7, 2, 3, 9, 1, 4, 0] random.shuffle(a) print(a) # [8, 4, 9, 2, 6, 3, 5, 7, 0, 1] random.shuffle(a) print(a) # [1, 0, 7, 2, 4, 9, 5, 8, 3, 6] random.shuffle(a) print(a) # [4, 6, 0, 5, 1, 3, 9, 2, 7, 8]
As the elements are randomly reordered, the result will look different when you’ll execute the same code snippet. Like any random function, the behavior is non-deterministic. Please also note that the list itself is shuffled—there’s no new list created.
You can try it yourself in the following interactive Python shell:
How to Shuffle and Return a New List?
random.shuffle(a) method modifies an existing list
a. It works in-place. So, what if you want to shuffle the original list but return a new list while leaving the original list elements unchanged?
No problem—use slicing to copy the list.
import random a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b = a[:] random.shuffle(b) print(b)
Exercise: Take a guess—what’s the output of this code snippet? You can check your guess in our interactive code shell:
- How to copy a list?
- Python Lists [Ultimate Guide]
- The random module explained
- How to Shuffle Two Arrays in Unison in Python?
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.