In this tutorial, we will look at how to randomly select an item from a python list. We will also look at how to randomly select multiple values and select values with/without replacement from a list.
Note that the methods mentioned can be used for other sequences like tuples, strings, etc.
Randomly select an item from a list
To randomly select an item from a list, you can use the random.choice()
function in python. The following example illustrates its usage.
import random # create a list ls = [11, 12, 13, 14, 15] # randomly select a value val = random.choice(ls) # display the value print(val)
Output:
13
We get 13 as the result. If you try running the same code multiple times, you will get different values from the list.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Randomly select multiple values from a list
The random
module in python comes with handy functions to randomly select multiple values from sequences like lists with or without replacement.
Select values with replacement
Use the random.choices()
function to return values with replacement from a list. It means that the values once sampled can be used for further sampling. It returns a k-sized (k is user-defined) list of elements with replacement from the given sequence.
Let’s use it to get 3 values with replacement from the list ls created above. Pass the list to sample from and k (the number of values to sample) to the function.
# display the original list print(ls) # randomly select 3 values with replacement from the list vals = random.choices(ls, k=3) # display the result print(vals)
Output:
[11, 12, 13, 14, 15] [11, 15, 11]
We get 11, 15, and 11 as the three values randomly sampled. Note that we get 11 twice which can only happen if sampling with replacement. You can also use this function to select a single value with replacement from the list, just pass k=1.
Select values without replacement
Use the random.sample()
function to return values without replacement from a list. It means that the values once sampled can’t be used for further sampling. It returns a k-sized (k is user-defined) list of elements with replacement from the given sequence.
Let’s use it to get 3 values without replacement from the list ls. Pass the list to sample from and k (the number of values to sample) to the function.
# display the original list print(ls) # randomly select 3 values without replacement from the list vals = random.sample(ls, k=3) # display the result print(vals)
Output:
[11, 12, 13, 14, 15] [11, 13, 12]
We get 11, 13, and 12 as the values sampled. Note that all of them are different, this is because we are sampling without replacement. You can also use this function to select a single value without replacement from the list, just pass k=1.
The three methods shown in this tutorial – random.choice()
, random.choices()
, and random.sample()
work similarly for other sequence types such as tuples, strings, etc.
# list print(random.choice([1, 2, 3, 4])) # tuple print(random.choice(('one', 'two', 'three', 'four'))) # string print(random.choice('abcd'))
Output:
3 one b
For more on the random
module in python, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and the results presented here have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.