Randomly select elements from list without repetition in Python

29 Aug, 2020
Python’s built-in module in random module is used to work with random data. The random module provides various methods to select elements randomly from a list, tuple, set, string or a dictionary without any repetition. Below are some approaches which depict a random selection of elements from a list without repetition by:
Method 1: Using random.sample()
Using the sample() method in the random module. The sample() is an inbuilt method of the random module which takes the sequence and number of selections as arguments and returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. It is used for random selection from a list of items without any replacement.Example 1:
# importing the required moduleimport random  # list of itemsList = [10, 20, 30, 40, 50, 40,        30, 20, 10]  # using the sample() methodUpdatedList = random.sample(List, 3)  # displaying random selections from # the list without repetitionprint(UpdatedList)
Output:
[50, 20, 10]

We can also use the sample() method on a sequence of numbers, however, the number of selections should be greater than the size of the sequence.Example 2:
# importing the required moduleimport random  # using the sample() method on a# sequence of numbersUpdatedList = random.sample(range(1, 100), 5)  # displaying random selections without# repetitionprint(UpdatedList)
Output:
[51, 50, 97, 22, 6]

Method 2: Using random.choices()
Using choices() method in the random library, The choices() method requires two arguments the list and k(number of selections) returns multiple random elements from the list with replacement. However, we need to convert the list into a set in order to avoid repetition of elements.Example 1:
# importing the required moduleimport random  # converting the list into a setSet = set([10, 20, 30, 40, 50, 40,          30, 20, 10])  # using the choices() method on the # given datasetUpdatedList = random.choices(list(Set), k = 3)  # displaying random selections without # repetitionprint(UpdatedList)
Output:
[30, 20, 40]

If the choices() method is applied on a sequence of unique numbers than it will return a list of unique random selections only if the k argument (i.e number of selections) should be greater than the size of the list.Example 2:
# importing the required moduleimport random  # converting the list into setList = [i for i in range(1, 100)]  # using the choices() method on a# sequence of numbersUpdatedList = random.choices(List, k = 5)  # displaying random selections without# repetitionprint(UpdatedList)
Output:
[46, 32, 85, 12, 68]

Method 3: Using random.choice()
Using the choice() method in random module, the choice() method returns a single random item from a list, tuple, or string.Below is program where choice() method is used on a list of items.Example 1:
# importing the required moduleimport random  # list of itemsList = [10, 20, 30, 40, 50, 40,         30, 20, 10]  # using the choice() method to return a# single item from the datasetprint(random.choice(List))
Output:
20

Below is a program where choice method is used on sequence of numbers.Example 2:
# importing the required moduleimport random  # using the choice() method to return a# single item from the datasetprint(random.choice(range(1, 100)))
Output:
56
Python list-programs
python-list
Python-random
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Iterate over a list in Python