Python Friday #67: Generate Random Data From a Sequence

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:

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:

We can get as many items back as we like, even when that exceeds the number of items in our sequence:

 

Select multiple non-repeated items at random

If we need to make sure that items are not repeated, we can use the sample() function:

Since the elements do not repeat, we get an exception if we try to retrieve more elements than are present in the sequence:

 

Shuffle your whole sequence

If we need to put the elements of our sequence in a random order, we can use the shuffle() function:

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”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.