How to Random Select from a List in Python

0
429
How to Select Random Item From a List in Python

Getting random elements from the list is the most used task because you need a randomized result from a list of recommendations.

What is random sampling in Python?

Random sampling in Python is a process used to select a data set from a population where the probability of choosing each data set is the same.

Python random module

The random module is a built-in module that contains methods to return random values. Python also provides another module like secrets which you can use in cryptographic applications.

Let’s see how to select a single random value or multiple random values from the list.

Python random select from a list

To select a random element from a list in Python, use the random.choice() method. The random choice() is a built-in method that returns a random item from the non-empty sequence-like list.

How to use random.choice() method?

To use the random.choice() method in Python, import the random package at the head of your file.

import random

To create a list, use the square brackets and add some elements.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

To select a random element from the Python list, use the random.choice() method and pass the list as an argument.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

print(random.choice(data))

Output

python3 app.py
astral

You can see that it selects one random element from the list and returns it. If you rerun the above code, then every time, it will return a different element.

➜ python3 app.py
infy
➜ python3 app.py
tcs
➜ python3 app.py
affle

Selecting multiple random items from a list in Python

To select multiple random elements from a Python list, use the random.sample() method. The random.sample() is a built-in Python method that returns a particular length of list chosen from a sequence.

The random.sample() method accepts two arguments: a list or set and the number of elements, and returns the sample elements.

How to use random.sample() method?

To use the sample() method in Python, import the random module in your program.

import random

We need three random elements from the list, and then we will pass 3 as the second argument, which suggests the number of elements you need in the list.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

print(random.sample(data, 3))

Output

python3 app.py
['infy', 'affle', 'astral']

You can use unpacking if you want a more pythonic one-liner for selecting multiple items.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

random_item_first, random_item_second = random.sample(data, 2)

print(random_item_first)
print(random_item_second)

Output

tcs
astral

Select random elements for cryptography or security uses

With Python 3.6, you can now use the secrets module for selecting random elements for cryptography or security use cases.

The secrets module generates cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

To use the secrets module in Python, you need to import the package into your program.

import secrets

To select the random secure elements, use the secrets.random() method.

import secrets

data = ["infy", "tcs", "affle", "dixon", "astral"]

el = secrets.choice(data)

print(el)

Output

astral

And it will give us the same output as the random.choice() method but in a more secure way.

To generate a passphrase for your password or other cryptographic uses, the secrets module is preferable to a random module.

Select a random index of an element in a Python List

To select a random element’s index in the Python list, use the random.randrange() function. The randrange() function returns a randomly selected element from the specified range. Then use the len() method to find the object’s length.

import random

data = ["infy", "tcs", "affle", "dixon", "astral"]

el_index = random.randrange(len(data))

print(el_index)

Output

python3 app.py
3

If you rerun the above program, it will return a different index.

Using np.random.choice() Method

To select random elements from the Python array, use np.random.choice() method. To generate a random sample from a given 1D array, use the numpy.random.choice() method.

To use the np.random.choice() method, import the numpy module.

import numpy as np

To create an array in Python, use the np.array() method and pass the list in the argument.

import numpy as np

data = ["infy", "tcs", "affle", "dixon", "astral"]

arr = np.array(data)

To select random elements from the array, use np.random.choice() method.

import numpy as np

data = ["infy", "tcs", "affle", "dixon", "astral"]

arr = np.array(data)
el = np.random.choice(arr)

print(el)

Output

astral

As you can see from the output, it returns the random item from the array. So if you rerun it every time, it will return the random elements.

Conclusion

The best way to select a random element from a list is to use random.choice() method. To select multiple random elements from a list, use the random.sample() method.

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

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