I am wondering if there is a good way to "shake up" a list of items in Python. For example [1,2,3,4,5]
might get shaken up / randomized to [3,1,4,2,5]
(any ordering equally likely).
In Python, you can shuffle (= randomize) a list, string, and tuple with random. shuffle() and random. sample() . random.
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.
from random import shuffle
list1 = [1,2,3,4,5]
shuffle(list1)
print list1
---> [3, 1, 2, 4, 5]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With