python shuffle list
To shuffle a list in Python, you can use the random.shuffle()
function from the random
module. This function randomly reorders the elements of the list. Here's an example:
import random
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list)
This code will output a shuffled version of the list my_list
. Keep in mind that the shuffle()
function modifies the list in-place, meaning that it shuffles the elements directly within the list object without creating a new list.