Python's random.choice() function can be used to get a random item from a list. This is a useful function for when you need to choose a random item from a list for a game or other application. The function takes a list as an argument and returns a random item from the list.
You need to import random module of Python to use the choice() function
Syntax
random.choice(List)
Code example
import random
# create a list that contains names
names = ["John", "Rick", "Carol", "Daryl", "Carl"]
# pick a random name from the above list
result = random.choice(names)
print(result)
Output
Carol
Remember that the output will be different every time you run the above code.
The Python code example above imports the random module. The code then creates a list that contains names. The code then uses the random.choice() method to pick a random name from the list. The code then prints the name that was chosen.
Python's random.sample() function is a great way to get a random item from a list. This function takes a list as an input and returns a random element from the list. This is a great way to get a random item from a list if you don't know what item you want.
Syntax
random.sample(List, [int]numbers_of_rand_items)
Code example
import random
# create a list that contains names
numbers = [10, 20, 30, 40, 50, 60, 70]
# pick a random number from the list
result = random.sample(numbers, 1)
print(result[0])
Output
70
In the above code example
We are getting 1 random item using the above code example. If you want to get two random items each time the code executes using random.sample() function then you can use the below code.
random.sample(numbers, 2)
Output
[20, 50]
The code returns the list of two items.
Python's secrets module provides a function for generating random values: secrets.choice(). This function can be used to get a random item from a list in Python.
To use secrets.choice(), you first need to import the module:
import secrets
fruits = ['Apple', 'Orange', 'Papaya', 'Grapes', 'Banana']
result = secrets.choice(fruits)
print(result)
Output
Grapes
This code example is importing the secrets library and using the choice function to select fruit from a list of fruits at random. The result is printed on the console.
If you need to select a random element from a list in Python, you can use the random.randrange() function. This function takes two arguments, the first is the start of the range and the second is the end of the range. The function will return a random integer from the start to the end, inclusive.
Syntax
random_index = random.randrange(len(List my_list))
my_list[random_index]
Code example
import random
# create a list
fruits = ['Apple', 'Orange', 'Papaya', 'Grapes', 'Banana']
print("Input List is: ", fruits)
# get a random index
rand_index = random.randrange(len(fruits))
print("Random item is: ", fruits[rand_index])
Output
Input List is: ['Apple', 'Orange', 'Papaya', 'Grapes', 'Banana']
Random item is: Papaya
The code above creates a list of fruits and then prints the list to the console. It then uses the randrange() function to get a random index from the list. Finally, it prints the fruit at the random index to the console.
The random.randint() function can be used to get a random item from a list in Python. This function takes two arguments, the lower and upper bounds of the range from which the item will be selected. The selected item will be returned as an integer.
Code example
import random
# create a list
fruits = ['Apple', 'Orange', 'Papaya', 'Grapes', 'Banana']
print("Input List is: ", fruits)
# get a random index
rand_index = random.randint(0, len(fruits)-1)
rand_item = fruits[rand_index]
print("Random item is: ", rand_item)
Output
Input List is: ['Apple', 'Orange', 'Papaya', 'Grapes', 'Banana']
Random item is: Orange
The code creates a list containing various fruits. It then generates a random number between 0 and the length of the list - 1. This random number is used as an index to retrieve a random element from the list.
0 Comments