Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomizing a list in Python [duplicate]

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).

like image 911
amrcsu Avatar asked Jan 18 '16 19:01

amrcsu


People also ask

Can I randomize a list in Python?

In Python, you can shuffle (= randomize) a list, string, and tuple with random. shuffle() and random. sample() . random.

How do you jumble a list in Python?

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.


1 Answers

from random import shuffle

list1 = [1,2,3,4,5]
shuffle(list1)

print list1
---> [3, 1, 2, 4, 5]
like image 55
roganjosh Avatar answered Sep 28 '22 08:09

roganjosh