You can use a method like random.choice(), random.randint(), random.sample(), random.choice(), random.random() in Python, to randomly select an item from a list.
A list is used to store multiple values in a single variable. The list may contain mixed
types of data, and in this article, we will explore how to select random items from a list.
Table of Contents:
Methods to Randomly Select an Item from a List in Python
Below are a few methods to randomly select an item from a list in Python:
Method 1: Using random.choice() in Python
This method is used to return the random element from a list, tuple, or string, and it is an inbuilt function.
Example:
Output:
Explanation: You get the random elements from the list using the random.choice() module.
Method 2: Using random.randrange() in Python
Let’s say you have some range of list length, and this function is used to generate the random index out of it. And we use this random index to get that element.
Example:
Output:
Explanation: The random.randrange() is used in this code, to generate the random index. And you will get the item using the generated index.
Method 3: Using random.random() function in Python
The floating-point numbers produced by this method fall between 0 and 1. t doesn’t need any input and gives random numbers between 0 and 1, all with equal chances.
Example:
Output:
Explanation: This Python code selects a random element from the list_a by generating a random index. It then prints the selected element.
Method 4: Using random.sample() in Python
Using this function, it is easy to choose multiple items from a list or just one item from a specific sequence.
Example:
Output:
Explanation: This Python code creates a list of programming languages and frameworks. It then prints a randomly shuffled list of all five elements in list_a.
Method 5: Using random.randint() in Python
It works the same as the random.randrange() function, it only differs by having two mandatory arguments. Other than that, it generates a random number in the given range.
Example:
Output:
Explanation: This Python code generates a random index within the range of the list_a elements. It then accesses and prints the element at the randomly generated index.
Method 6: Using random.choices() in Python
This method randomly selects the multiple elements and also supports the repetition of the element to get returned.
Example:
Output:
Explanation: This Python code selects three random elements from the list list_a, allowing repetition.
Method 7: Select k random value from a list
To select a random value, a random.sample() can be used. This function allows you to choose more items without repeating them.
Example:
Output:
Explanation: The code randomly selects 3 elements from a list, then checks if the list length is divisible by 3 and groups the elements accordingly. If it’s not divisible, it notifies that proper pairing isn’t possible.
Method 8: Using random.shuffle() in Python
The random.shuffle() modifies the list in place and we take the first n elements after shuffling.
Example:
Output:
Explanation: Your code shuffles a list of elements and then selects the first three elements from the shuffled list.
Method 9: Using secret.choice() in Python
It is a more secure way to randomly select an item from a list in Python. This is used to avoid predictable randomness.
Example:
Output:
Explanation: The code initializes a list of strings and then uses the secrets module to randomly and securely select one item from the list. Finally, it prints the randomly selected item.
Methods | Performance | Pros | Cons |
random.choice() | O(1) | Fast and simple, | No support for multiple selections |
random.randrange() | O(1) | Flexible for range-based selection | Requires indexing manually |
random.random() | O(1) | Flexible, can be combined for scaling | More complex, requires typecasting |
random.sample() | O(k) | Great for non-repeating samples | Inefficient for single selections |
random.randint() | O(1) | Simple to use for range-based indexing | Requires manual indexing |
random.choices() | O(k) | Supports repetition | Less efficient for non-repeated items |
random.shuffle() | O(n) | Great for list reordering | Modifies the list, computationally expensive |
secrets.choice() | O(1) | Cryptographically secure | Slightly slower, overkill for non-secure tasks |
Conclusion
The random.choice() is a built-in function, and this function is used to return randomly. The random.randrange() function is used to return random elements based on their index. The random.randint() function resembles the random.randrange() function and random.choices() function returns the random element with repetition. The choice of method is based on your requirements.