How to randomly shuffle a list in Python?

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

import random

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

Example output:

[3, 2, 1, 5, 4]

Please note that the shuffle function will directly modify the original list instead of returning a new list. If you need to keep the original list, you can first create a copy before shuffling it.

import random

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

Example Output:

[1, 2, 3, 4, 5]
[3, 2, 1, 5, 4]
bannerAds