# Python Select Random Item from List with random.choice

> Select a random item from a Python list using random.choice for general use or secrets.choice for cryptographically secure random selection

**URL:** https://sentry.io/answers/select-a-random-item-from-a-list-in-python/

---

## The Problem

In Python, how can I select one item from a list at random?

## The Solution

Python's built-in [`random`](https://docs.python.org/3/library/random.html) module provides the [`choice`](https://docs.python.org/3/library/random.html#random.choice) function for this purpose. For example:

```python
import random

my_list = [1, 2, 3, 4, 5, 6]
randomly_selected = random.choice(my_list)

print(randomly_selected)
# will print a pseudorandomly selected number from my_list every time the script is run
```

As noted in a warning near the top of [the `random` module's documentation](https://docs.python.org/3/library/random.html), **values produced by its generators should not be used for security purposes**.

If we're using our randomly chosen value for modeling a system or building a game, the method above will be fine, but if we need more robust randomness for things like passwords and security tokens, we'll need to replace `random` with Python's [`secrets`](https://docs.python.org/3/library/secrets.html#module-secrets) module. Fortunately, this module also contains a [`choice`](https://docs.python.org/3/library/secrets.html#secrets.choice) function:

```python
import secrets

my_list = [1, 2, 3, 4, 5, 6]
randomly_selected = secrets.choice(my_list)

print(randomly_selected)
# will print a randomly selected number from my_list every time the script is run
```

---

*Source: [sentry.io/answers/select-a-random-item-from-a-list-in-python/](https://sentry.io/answers/select-a-random-item-from-a-list-in-python/)*
