How do you shuffle a list value in python?

In this article, you will learn how to shuffle a list using the Python programming language.

A list is a sequence of indexed and ordered values, like an array. It is mutable, which means we can change the order of elements in a list. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. Each element of the list has an index, starting with 0, 1, and so on. In the development process, we may come to a situation in which we need to shuffle a list.

Shuffling a list of numbers has consistently been a valuable utility and the question that has shown up in many organization interviews too. Knowing beyond what one strategy to accomplish this can generally be an or more. We should examine certain manners by which this can be accomplished.

Python random.shuffle()

The random module of Python provides an inbuilt method shuffle() to randomize the items of lists, strings, and tuples. This method shuffles the original items of lists, strings, and tuples. This is the most recommended method to shuffle a list.

Syntax of shuffle()

shuffle (sequence,[random])

Here, the list is any sequence or tuple that you want to shuffle, and random is the optional parameter. It is a function returning a random floating number between 0.1 and 1.0. If not specified, by default it takes a random.random() function.

Python shuffle a range value

In the given example, we have used a random module to perform the random generations on a list and have taken a range of 7 numbers and used the random.shuffle() method to shuffle the given list in place.

import random

x = list(range(7))
print(x)

random.shuffle(x)
print(x)

Output of the above code:

[0, 1, 2, 3, 4, 5, 6]
[0, 1, 6, 2, 3, 5, 4]
[0, 1, 2, 3, 4, 5, 6]
[2, 6, 0, 3, 1, 5, 4]

Python shuffle a list

In the given Python program, we have initialized the list items and shuffled them using the random.shuffle() method.

import random

list1 = [34, 35, 73, 22, 13, 6, 23, 7, 31, 70]
print("Original list:", list1)

random.shuffle(list1)
print("First shuffled list:", list1)

random.shuffle(list1)
print("Second shuffled list:", list1)

Output of the above code:

Original list: [34, 35, 73, 22, 13, 6, 23, 7, 31, 70]
First shuffled list: [13, 70, 73, 7, 22, 31, 35, 23, 34, 6]
Second shuffled list: [22, 23, 7, 6, 13, 73, 31, 34, 70, 35]

Python random.sample()

In the above examples, you have seen that the random.shuffle() method shuffles the original lists. But what if we want to shuffle not in-place, then we need to use a random.sample() function. The random.sample() method returns a new shuffled list. The original list remains unchanged.

Syntax of random.sample()

random.sample(sequence, n)

Here, the sequence can be any sequence, list, tuple, string that you want to shuffle, and n is the integer value that specifies the length of a sample. It returns the random list with the sample size you passed to it. For example, sample(number_list, 5) will return a list of 5 random items from a list.

Python example of random.sample()

In the given example, we have shuffled a list using the random.sample() method.

from random import sample 
  
number_list = [43, 12, 19, 6, 23, 17, 31]  
  
print("First shuffled list:", sample(number_list,5))

print("First shuffled list:", sample(number_list,3))

print("Original list:", number_list)

We got the following output-

First shuffled list: [12, 17, 6, 31, 23]
First shuffled list: [23, 17, 43]
Original list: [43, 12, 19, 6, 23, 17, 31]

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.

How do you shuffle a list value in 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!

How do you shuffle a list value in 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.

How do you shuffle data in a list in Python?

Python Random shuffle() Method The shuffle() method takes a sequence, like a list, and reorganize the order of the items. Note: This method changes the original list, it does not return a new list.

How can you randomize the items of a list in place in Python?

The shuffle() method randomizes the items of a list in place.

How do you randomize in Python?

Random integer values can be generated with the randint() function. This function takes two arguments: the start and the end of the range for the generated integer values. Random integers are generated within and including the start and end of range values, specifically in the interval [start, end].

Do we have any inbuilt function to shuffle the values of the list?

Python3. This is most recommended method to shuffle a list. Python in its random library provides this inbuilt function which in-place shuffles the list.