Python

Python Randomly Select From List

There are a lot of packages and methods available in Python to select some values from the list due to the diversity of this language. The most known packages are “random” and “secret”. Today, we will be looking at some examples of built-in methods to choose a random item from the list data structure within Ubuntu 20.04. Start by opening the shell console with “Ctrl+Alt+T”.

Note: The examples demonstrated in this guide are implemented on Ubuntu 20.04.

Example 01: Random.choice() Method

The very first and most used method to get the random value from a list is using the built-in random package of Python. It only selects a single random value. To use it in your code, create a new Python file, i.e., “new.py” with the “touch” keyword. This file can be opened in the editor to create code, i.e., Nano editor:

$ touch new.py

$ nano new.py

Add the Python support in the newly created file. Now, import the “random” package of Python and initialize a list. The list can be 1 integer or string type. This list will be passed to the choice() method of the random package as an argument, and a random value will be selected. This selected random value will be printed out with the help of a print clause:

#! /usr/bin/python

Import random

List = [ 2, 4, 6, 8, 10]

Print(random.choice(List))

Run the code file with the “python3” keyword. You can see that the choice() method or random package has selected 4 from the list:

$ python3 new.py

If you are using some set as a list value, you have to make it a tuple first. So, we have initialized another string-type array and converted it to a tuple. This converted tuple will be passed to the choice() method to select a random element. The selected random number has been saved to another variable and printed out:

#! /usr/bin/python

Import random

List = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]

E= random.choice(tuple(List))

Print(e)

Upon executing the file new.py several times, we came up with different random numbers each time:

$ python3 new.py

Example 02: Random.choices() Method

The only difference between the choice() and choices() method of the random package is “s”. The choices() method would be selecting more than 1 random value from the list. So, we have opened the new.py file and updated it a little. After initializing a list, we have used the choices() method on the list while defining the number of values randomly selected from the list, i.e., “k”. These randomly selected multiple values will be saved to

#! /usr/bin/python

Import random

List = [ 2, 4, 6, 8, 10]

Print(random.choice(List))

Upon the execution of the new.py file, we have got 4 random values from the list as displayed:

$ python3 new.py

Example 03: Random.sample() Method

The sample() method of the random package works the same as the choices() method does. So, we have used the same list to get the random value. The print statement utilizes the sample() method to get the random numbers from a list. The list and number of random values to be chosen are passed to the sample() method. The output will show the 3 random values:

#! /usr/bin/python

Import random

List = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]

Print(“ Random Element:”, random.sample(List, 3))

Upon running the updated Python file, we have three random alphabets as values

$ python3 new.py

Example 04: Secrets.choice() Method

Other than a random package, the secret package can also be utilized for selecting the random value from a list. The secret package also uses the “choice()” method to do so. So, we have imported the secrets package into the code and used the same list. The print statement has been updated with the use of secrets.choice() function on the list:

#! /usr/bin/python

Import secrets

List = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]

Print(“ Random Element:”, secrets.choice(List))

Executing the code several times leads us to different outputs:

$ python3 new.py

Conclusion:

We have discussed several examples to get the random element from a list data structure in Python. Other than that, you can also use the range() method to get the random value. We are sure that this guide will clear all your doubts. We hope you found this article helpful. Please check out more informative articles at Linux Hint.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content