Randomly select elements from list without repetition in Python

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:

Python3

 `# importing the required module``import` `random` `# list of items``List` `=` `[``10``, ``20``, ``30``, ``40``, ``50``, ``40``,``        ``30``, ``20``, ``10``]` `# using the sample() method``UpdatedList ``=` `random.sample(``List``, ``3``)` `# displaying random selections from ``# the list without repetition``print``(UpdatedList)`

Output:

`[50, 20, 10]`

Time Complexity: O(n) where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list

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:

Python3

 `# importing the required module``import` `random` `# using the sample() method on a``# sequence of numbers``UpdatedList ``=` `random.sample(``range``(``1``, ``100``), ``5``)` `# displaying random selections without``# repetition``print``(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:

Python3

 `# importing the required module``import` `random` `# converting the list into a set``Set` `=` `set``([``10``, ``20``, ``30``, ``40``, ``50``, ``40``,``          ``30``, ``20``, ``10``])` `# using the choices() method on the ``# given dataset``UpdatedList ``=` `random.choices(``list``(``Set``), k ``=` `3``)` `# displaying random selections without ``# repetition``print``(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:

Python3

 `# importing the required module``import` `random` `# converting the list into set``List` `=` `[i ``for` `i ``in` `range``(``1``, ``100``)]` `# using the choices() method on a``# sequence of numbers``UpdatedList ``=` `random.choices(``List``, k ``=` `5``)` `# displaying random selections without``# repetition``print``(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:

Python3

 `# importing the required module``import` `random` `# list of items``List` `=` `[``10``, ``20``, ``30``, ``40``, ``50``, ``40``, ``        ``30``, ``20``, ``10``]` `# using the choice() method to return a``# single item from the dataset``print``(random.choice(``List``))`

Output:

`20`

Below is a program where choice method is used on sequence of numbers. Example 2:

Python3

 `# importing the required module``import` `random` `# using the choice() method to return a``# single item from the dataset``print``(random.choice(``range``(``1``, ``100``)))`

Output:

`56`

Previous
Next