
To pick a random element from a list in Python, use the random module’s choice() method:
import random names = ["Alice", "Bob", "Charlie"] print(random.choice(names))
Output (example):
Bob
This provides you with a quick answer.
However, there are other ways you can consider.
Let’s take a deeper look at randomly picking elements from lists in Python.
6 Ways to Get Random Element from a List in Python
There are six different ways to select elements randomly from a list in Python:
- random.randrange()
- random.random()
- random.choice()
- random.randint()
- random.sample()
- secrets.choice()
Let’s go through each of these methods in more detail with useful examples.
1. Random.randrange()
To choose a random value between two numbers, you can use the random.randrange() function.
To utilize this to pick a random element from a list:
- Generate a random number index between 0 and the length of the list.
- Get an element from a list based on that index.
For example:
import random names = ["Alice", "Bob", "Charlie", "David"] random_index = random.randrange(len(names)) random_name = names[random_index] print(random_name)
Output (example):
David
2. Random.random()
The random.random() method returns a random floating-point number between 0 and 1.
Even though it is not that practical, you can use this method to pick a random element from a list.
For example:
import random names = ["Alice", "Bob", "Charlie", "David"] random_index = int(random.random() * len(names)) random_name = names[random_index] print(random_name)
Output (example):
Alice
3. Random.choice()
The random.choice() is the go-to method for picking items from a list at random.
For example:
import random names = ["Alice", "Bob", "Charlie", "David"] random_name = random.choice(names) print(random_name)
Output:
David
4. Random.randint()
To pick a random integer between two numbers, use the random.randint() method.
When it comes to picking a random element from a list, you can use random.randint() as follows:
- Pick a random integer index between 0 and the length of the list – 1.
- Use the randomized index to grab the corresponding element from a list.
For instance:
import random names = ["Alice", "Bob", "Charlie", "David"] random_index = random.randint(0, len(names) - 1) random_name = names[random_index] print(random_name)
Output:
Alice
5. Random.sample()
Last but not least, you can use the random.sample() method to pick a group of random elements from a list.
The syntax:
random.sample(list, num_elements)
Where:
- list is the group of items from where you are picking values
- num_elements is the number of random elements you want to sample from the list.
For example, let’s choose two random names from a group of four:
import random names = ["Alice", "Bob", "Charlie", "David"] random_names = random.sample(names, 2) print(random_names)
Output example:
['Bob', 'Charlie']
6. Secrets.choice()
To pick a cryptographically strong random value from a list, use the secrets.choice() method.
This method was added in Python 3.6.
For example:
import secrets names = ["Alice", "Bob", "Charlie", "David"] random_name = secrets.choice(names) print(random_name)
Output:
Alice
However, unless you really need a cryptographically secure random number generator, use the random.choice() method instead.
Conclusion
There are a lot of ways to pick random items from a list in Python.
To take home, use the random.choice() method for picking a random element from a list. The other methods work fine, but the random.choice() method is meant for exactly that.