While programming in python, we may need to select a random element from a list in several situations. In this article, we will discuss different ways to select an element from a list in python.
Select Random Element from A List using the random module
The random
module in python provides us with different function to generate random numbers. We can also select random elements from a list using the functions defined in this module.
To select a random element from a list in python, we can use the choice()
function defined in the random
module. The choice()
function takes a list as input and returns a random element from the list every time it is executed.
You can observe this in the following example.
import random
myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = random.choice(myList)
print("The randomly selected element is:", random_element)
Output:
The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 8
Select Random Element from A List using the secrets module
The secrets
module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets. However, we can also use this module to select a random element from a list.
The choice()
function defined in the secrets module works the same way as the choice()
function defined in the random module. It takes a list as input and returns an element from the list as follows.
import secrets
myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = secrets.choice(myList)
print("The randomly selected element is:", random_element)
Output:
The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 45
Using the numpy module
We can also use the choice() function from the numpy
module to select a random element from a list. The choice()
function in the numpy
module works the same way as random
module or secrets
module. You can observe this in the following example.
import numpy
myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_element = numpy.random.choice(myList)
print("The randomly selected element is:", random_element)
Output:
The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected element is: 3
While using the numpy
module, we have a benefit that we can even make more than one random selection from the list. For this, we will use the “size
” parameter of the function. If we want to select n
random elements from a given list, we will pass the number n as the second input argument to the choice()
function defined in the numpy
module. Upon execution, the function returns a list of n
elements as shown below.
import numpy
myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_elements = numpy.random.choice(myList, 4)
print("The randomly selected elements are:")
for x in random_elements:
print(x)
Output:
The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected elements are:
78
3
6
23
Here,you can observe that an element can be selected more than once in the output list. To avoid this, we will use the third parameter i.e. “replace
” and set it to False
. After this, once an element is selected, it will not be considered for another selection. Hence, an element will appear only once in the output list. You can observe this in the following example.
import numpy
myList = [1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
print("The list is:")
print(myList)
random_elements = numpy.random.choice(myList, 4, replace=False)
print("The randomly selected elements are:")
for x in random_elements:
print(x)
Output:
The list is:
[1, 2, 3, 45, 6, 8, 78, 23, 56, 7686, 123]
The randomly selected elements are:
1
56
3
2
Conclusion
In this article, we have discussed several ways to select a random element from a list in python. We also saw how you can select more than one random elements from a list. To know more about lists in python, you can read this article on list comprehension. You might also like this article on string concatenation in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.