The usual way to pick a random item from a list is to generate a random number and pick the item at this position. That is how I would do it in C#. In Python we have much better ways to achieve that.
This post is part of my journey to learn Python. You can find the other parts of this series here.
Select an item at random
The choice() function in the random module accepts a sequence as a parameter and picks one item at random from it:
1 2 3 4 5 6 7 |
>>> import random >>> items = ['A','B','C'] >>> random.choice(items) 'A' >>> random.choice(items) 'C' >>> random.choice(items) |
I like this much better than the traditional approach with the random number to pick an element by its position. Not only do we need to write less code, the code we write is easier to understand.
Select multiple items at random
If we need multiple items at random from a sequence, we can use the choices() function. As with the singular choice, we need to provide the sequence to choose from and the named argument k to tell it how many items we want:
1 2 3 4 5 6 7 8 |
>>> import random >>> tuple = ('A','B','C') >>> random.choices(tuple, k=2) ['C', 'C'] >>> random.choices(tuple, k=2) ['A', 'C'] >>> random.choices(tuple, k=2) ['C', 'B'] |
We can get as many items back as we like, even when that exceeds the number of items in our sequence:
1 2 |
>>> random.choices(tuple, k=10) ['C', 'A', 'B', 'C', 'A', 'A', 'A', 'A', 'B', 'C'] |
Select multiple non-repeated items at random
If we need to make sure that items are not repeated, we can use the sample() function:
1 2 3 4 5 6 7 8 9 10 |
>>> import random >>> tuple = ('A','B','C') >>> random.sample(tuple, 2) ['C', 'A'] >>> random.sample(tuple, 2) ['A', 'C'] >>> random.sample(tuple, 2) ['C', 'B'] >>> random.sample(tuple, 2) ['C', 'A'] |
Since the elements do not repeat, we get an exception if we try to retrieve more elements than are present in the sequence:
1 2 3 4 5 6 7 8 |
>>> import random >>> tuple = ('A','B','C') >>> random.sample(tuple, 4) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\jg\AppData\Local\Programs\Python\Python38-32\lib\random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative |
Shuffle your whole sequence
If we need to put the elements of our sequence in a random order, we can use the shuffle() function:
1 2 3 4 5 6 7 8 9 10 11 |
>>> import random >>> items = ['A','B','C','D','E','F','G','H'] >>> random.shuffle(items) >>> items ['B', 'A', 'F', 'D', 'E', 'H', 'G', 'C'] >>> random.shuffle(items) >>> items ['G', 'A', 'F', 'C', 'E', 'H', 'D', 'B'] >>> random.shuffle(items) >>> items ['E', 'D', 'G', 'B', 'F', 'C', 'H', 'A'] |
With shuffle() you change the order of your sequence in-place, if that is not what you want, you need to create a copy of the sequence before you run shuffle.
Just the beginning
The random module offers a lot of functionality and I covered here and in the last post only the most basic use cases. There are more interesting things you can do and I strongly recommend to check the documentation to learn about them.
1 thought on “Python Friday #67: Generate Random Data From a Sequence”