How to shuffle a list randomly in Python?

You can use the shuffle function in the random module to randomize a list. Here is an example:

import random

my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list)

In this example, we first import the random module. Then we define a list called my_list. Next, we use the shuffle function from the random module to randomly shuffle the my_list list. Finally, we print out the shuffled list.

Please note that the shuffle function will modify the original list directly, instead of returning a new list.

bannerAds