python

Get a random item from a List in Python

If you're working with a list in Python, there are a number of ways you can get a random item from the list. You can use the random module, which has a number of functions for generating random numbers, or you can use the choice() method from the random module. You can also use the shuffle() method from the random module to shuffle the items in a list, and then use the pop() method to remove and return a random item from the list.

Solution 1: Get a random item from a Python list using random.choice() function

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.

Solution 2: Using random.sample() function

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

  1. The code imports the random module.
  2. It then creates a list called numbers that contains the integers from 10 to 70.
  3. The random.sample() function is then used to pick a random number from the list.
  4. The result is stored in a variable called result.
  5. Finally, the code prints the first element of the result list (which is the random number).

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.

Solution 3: Using secrets.choice() function

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.

Solution 4: Get random element from List using random.randrange() function

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.

Solution 5: Using random.randint() function

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.

Was this helpful?