Shuffle a list of lists python

Call 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.

How to Shuffle a List in Python?

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 random library.

Shuffle a list of lists python

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?

The default 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:

Related articles:

  • How to copy a list?
  • Slicing
  • 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.

Join the free webinar now!

Shuffle a list of lists python

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. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, 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.

Putting a list into random order might seem like an unusual task, but it can be quite useful for many businesses. For example, you might want to randomly assign leads to salespeople, assign jobs to employees or conduct a raffle for customers. Whatever the reason, you can use the popular programming language Python to randomize the order of a list of elements.

If you're generating random lists or random numbers for a sensitive purpose, such as a prize draw with money at stake or cryptographic purposes, you'll want to make sure you're using a high-quality random number generator.

Shuffle a List in Python

You can use Python to build a list randomizer tool in just a few lines of code. Python is free for both commercial and personal use and available for all modern operating systems, including Microsoft Windows, macOS, Linux and other Unix-style systems.

Python 2 and Python 3, the two versions of the programming language in widespread use, include a function called shuffle that can randomize a list or another sequence of data. To use shuffle, import the Python random package by adding the line import random near the top of your program.

Then, if you have a list called x, you can call random.shuffle(x) to have the random shuffle function reorder the list in a randomized way.

Note that the shuffle function replaces the existing list. If you want to keep a copy of the list in its original order, make a copy of the list before shuffling it. You can import the Python copy package and use its copy method to do so. Use y = copy.copy(x) to create a copy of list x and assign variable y to refer to it. Note that if you write y = x, variable y is assigned to point to the same list as x does, and a new copy is not created.

Grab a Random Element

If you only want to grab a random element from a list in Python, you can do this with the random package as well. Import the random package by including import random near the top of your code.

To choose a single element, use random.choice(x), where x is the name of your list. The function returns a single, randomly selected element from list x. Note that if you call random.choice multiple times, you may get the same element of the list multiple times unless you delete it from the list in between calls.

If you want to grab a set of multiple elements that doesn't include the same element twice, use random.sample (x, k), where x is the list and k is the number of elements you want. If a list includes repeated elements, they might be repeated in the randomized sample.

Risks With Random Number Generators

Not all random number generators are created equal. The Python documentation warns that Python's built-in random number generator isn't suitable for cryptographic purposes, where a minimum level of actual randomness is required to create random number generators to encrypt data with little risk of third-party decryption. If it's important to your business that the list is truly random and unpredictable, such as for a raffle drawing, it's important to use the right random number generator.

Some subclasses of the Python random module provide increased levels of randomness. On modern operating systems, you can ask the operating system to provide random data of a quality usable for cryptography. Through Python, you can access this random data using the function os.urandom in the os module, or you can call random.SystemRandom to generate a random number generator equivalent to the random module using the operating system random data.

To use this with shuffle, type r = random.SystemRandom() to generate the random number generator and then call r.shuffle(x) on your list x. Other functions, including choice and sample, can also be used with the SystemRandom generator.

Check your operating system's documentation to understand how its random number generator works and if it's suitable for your needs. In some cases, you may be able to configure the operating system to use an external hardware device to generate random numbers.

How do you shuffle multiple lists in Python?

Method : Using zip() + shuffle() + * operator In this method, this task is performed in three steps. Firstly, the lists are zipped together using zip(). Next step is to perform shuffle using inbuilt shuffle() and last step is to unzip the lists to separate lists using * operator.

Can you shuffle a list in Python?

shuffle() function in Python. The shuffle() is an inbuilt method of the random module. It is used to shuffle a sequence (list). Shuffling a list of objects means changing the position of the elements of the sequence using Python.

How do you shuffle a list order in Python?

To shuffle strings or tuples, use random. sample() , which creates a new object. random. sample() returns a list even when a string or tuple is specified to the first argument, so it is necessary to convert it to a string or tuple.

How do you randomly permute a list in Python?

The random. sample() function returns the random list with the sample size you passed to it. For example, sample(myList, 3) will return a list of 3 random items from a list. If we pass the sample size the same as the original list's size, it will return us the new shuffled list.