In this tutorial, I will explain how to randomly select from a list in Python. Recently, I faced this issue while working on a project that involved selecting random names for a customer feedback survey, and I found several techniques that can be very useful. Let us learn more about this topic with suitable examples and screenshots.
Randomly Select from a List in Python
Before we explore the methods for random selection from a list, let’s clarify what we mean by “random selection.” In programming, random selection refers to choosing an item from a list where each item has an equal chance of being chosen. In Python, the random module provides several functions that help with this task.
To randomly select an item from a list in Python, you can use the random.choice() function from the random module. For example, if you have a list of names like ["Alice", "Bob", "Charlie"] you can randomly select one name by calling random.choice(names) , which might return “Alice”, “Bob”, or “Charlie” each time you run the code.
Read How to Remove Duplicates from a List in Python?
The random Module in Python
Python’s built-in random module offers several functions for random operations. The most commonly used functions for selecting random items from a list are:
random.choice(): Selects a single random item from a non-empty sequence.random.sample(): Returns a list of unique random items from a sequence.random.choices(): Returns a list of random items from a sequence, allowing for duplicates.
Import the Random Module
To use these functions, you first need to import the random module. Here’s how you do it:
import randomCheck out How to Add Tuples to Lists in Python?
1. Use random.choice()
The random.choice() function in Python is the simplest way to select a single random item from a list. Let’s see how it works with a practical example.
Example 1: Select a Random Name
Suppose we have a list of popular names in the USA, and we want to randomly select one name for a greeting message.
import random
names = ["Michael", "Jessica", "Joshua", "Ashley", "Matthew", "Amanda"]
random_name = random.choice(names)
print(f"Randomly selected name: {random_name}")Output:
Randomly selected name: JessicaYou can see the output in the screenshot below.

In this example, the random.choice() function picks one name from the list names and we print it out. Each time you run this code, you might get a different name.
Read How to Convert Dictionary to List of Tuples in Python?
2. Use random.sample()
If you need to select multiple unique items from a list, random.sample() in Python is the way to go. This function allows you to specify how many items you want to select, and it ensures that the selected items are unique.
Example 2: Select Multiple Unique Names
Let’s modify our previous example to select three unique names from the list.
import random
names = ["Michael", "Jessica", "Joshua", "Ashley", "Matthew", "Amanda"]
random_names = random.sample(names, 3)
print(f"Randomly selected names: {random_names}")Output:
Randomly selected names: ['Amanda', 'Jessica', 'Ashley']You can see the output in the screenshot below.

In this case, random.sample(names, 3) selects three unique names from the names list. If the list has fewer items than the number you want to select, Python will raise a ValueError.
Check out How to Split a Python List Into Evenly Sized Chunks?
3. Use random.choices()
If you need to select multiple items from a list and allow for duplicates, the Python random.choices() function is perfect. This function can also take a weights parameter if you want to influence the selection probability.
Example 3: Select Names with Duplicates
Let’s say we want to select five names from our list, and we’re okay with duplicates.
import random
names = ["Michael", "Jessica", "Joshua", "Ashley", "Matthew", "Amanda"]
random_names = random.choices(names, k=5)
print(f"Randomly selected names (with possible duplicates): {random_names}")Output:
Randomly selected names (with possible duplicates): ['Ashley', 'Jessica', 'Matthew', 'Ashley', 'Ashley']You can see the output in the screenshot below.

Here, random.choices(names, k=5) returns a list of five names, which may include duplicates.
Read How to Find the Index of the Maximum Value in a List using Python?
Customize Random Selection with Weights
One powerful feature of random.choices() is the ability to assign weights to the items. This allows you to control the likelihood of each item being selected.
Example 4: Weighted Random Selection
Let’s say we have a list of names, and we want to give more weight to certain names based on their popularity.
import random
names = ["Michael", "Jessica", "Joshua", "Ashley", "Matthew", "Amanda"]
weights = [1, 3, 1, 2, 1, 1] # Jessica is more likely to be chosen
random_name = random.choices(names, weights=weights, k=1)
print(f"Randomly selected name with weight: {random_name[0]}")In this example, Jessica has a higher chance of being selected due to her weight being set to 3, while the others have weights of 1.
Check out How to Capitalize the First Letter of Every Word in a List using Python?
Applications of Random Selection
Let us see some practical applications of random selection:
Randomly Assigning Tasks
Imagine you have a list of team members in a project, and you want to assign tasks randomly. You can use random.sample() to ensure that each member gets a task without repetition.
import random
team_members = ["Alice", "Bob", "Charlie", "Diana", "Ethan"]
tasks = ["Design", "Development", "Testing", "Deployment"]
assigned_tasks = random.sample(list(zip(team_members, tasks)), len(tasks))
for member, task in assigned_tasks:
print(f"{member} is assigned to {task}.")Randomly Selecting Survey Participants
In a survey scenario, you might want to randomly select participants from a larger group to ensure a fair representation.
import random
participants = ["John", "Sarah", "David", "Emily", "Chris", "Anna", "Tom"]
selected_participants = random.sample(participants, 3)
print(f"Selected participants for the survey: {selected_participants}")Read How to Check if Any Element in a List is Present in Another List using Python?
Conclusion
In this article, I explained how to randomly select from a list in Python. I discussed the random module and three methods, such as using the random.choice() , using the random.sample() , and random.choices(). I also discussed how to customize random selection with weights and applications of random selection.
You may read:
- How to Sort a List in Python Without Using the sort() Function?
- How to Find the Closest Value in a List Using Python?
- How to Divide Each Element in a List by a Number in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.