Python How to Get a Random Element from a List

Python random element from list
Python random element from list

To pick a random element from a list in Python, use random module’s choice() method:

import random

names = ["Alice", "Bob", "Charlie"]
print(random.choice(names))

Output (example):

Bob

There are six different ways to select elements randomly from a list in Python:

  1. random.randrange()
  2. random.random()
  3. random.choice()
  4. random.randint()
  5. random.sample()
  6. secrets.choice()

Let’s go through each of these in more detail with 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 elemens 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.

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published.