How can I randomly select an item from a list in Python?

Better Stack Team
Updated on February 2, 2023

To randomly select an item from a list in Python, you can use the random.choice() function from the random module. This function takes a list as an argument and returns a randomly selected element from the list.

Here is an example of how you can use random.choice():

import random

# List of items
items = ['apple', 'banana', 'cherry', 'date', 'elderberry']

# Select a random item
selected_item = random.choice(items)


This code will output one of the items from the items list at random. The item that is selected will be different each time the code is run.

Note that you will need to import the random module at the beginning of your code in order to use the random.choice() function.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →