How to Shuffle a List in Python?

Estimated read time 1 min read

To shuffle a list in Python, you can use the random module, which provides a way to generate random numbers and shuffle lists. Here’s an example of how to shuffle a list in Python:

import random

# Define the list to be shuffled
mylist = [1, 2, 3, 4, 5]

# Shuffle the list
random.shuffle(mylist)

# Print the shuffled list
print(mylist)

In this example, the random.shuffle() function is used to shuffle the list mylist in a random order. Finally, the shuffled list is displayed on the console using the print() function.

When you run this code, you will see the list [1, 2, 3, 4, 5] shuffled in a random order. Note that this technique can be used to shuffle any list in Python, regardless of its length and content.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply