How to Choose Multiple Items From Any Sequence Using random.sample()?

Python provides a wide range of inbuilt functions and modules to perform different tasks and make code easier and faster. The inbuilt “random” module is one of them that is used for generating random numbers, choosing random items from a list, shuffling sequences randomly, etc.

The “random.sample()” function of the random module selects multiple items from any sequence, such as lists, tuples, and strings.

This blog provides an in-depth overview of how to choose multiple elements from any sequence using the “random.sample()” function.

Python random.sample() Function

The inbuilt function named “random.sample()” is used to choose/select multiple elements from the sequence, such as list, tuple, set, etc. This function retrieves the selected list of elements. 

The syntax of the “random.sample()” function is shown below:

Syntax:

random.sample(sequence, num)

In the above syntax, the “sequence” parameter specifies the input sequence, and the “num” parameter specifies the number of items that will be chosen from the sequence.

Let’s understand this method using the examples given below:

Example 1: Select Multiple Items From the List

To select multiple items/elements from the given list the following code is utilized in Python:

Code:

import random
list_value = [45, 55, 65, 75, 85]
output = random.sample(list_value, 3)
print(output)
  • The “random” module is imported.
  • The list named “list_value” is initialized.
  • The “random.sample()” takes the list and integer that represent the number of samples generated at the output as an argument. This function chooses 3 items from the given list.

Output:

The multiple items have been chosen from the given list.

Example 2: Select Multiple Items From Set

To choose multiple elements from the given set the following example is utilized:

Code:

import random
set_value = {'Joseph','Henry','Roger','Lily','Anna'}
print(random.sample(sorted(set_value), 3))
  • The “set” named “set_value” is initialized.
  • The “random.sample()” takes the sorted set value and specific integer as an argument to choose multiple items from the set.

Output:

The multiple items have been selected from the given set.

Example 3: Select Multiple Items/Elements From the Dictionary

The following example code is utilized to choose elements from the dictionary:

Code:

import random
dict_value = {'Name': 'Joseph', 'Age': 18, 'Height': 5.7,
              'Salary': 50000, 'Gender': 'Male'}
sorted_dict = sorted(dict_value.items())
print(random.sample(sorted_dict, 3))
  • The random module is imported.
  • The dictionary named “dict_value” is initialized.
  • The “dict.items()” function retrieves an object containing the key-value pairs of a dictionary as tuples in a list.
  • The “sorted()” function takes the “dict.items()” return value as an argument and sorts the list of tuples.
  • The “random.sample()” function takes the sorted value and specific integer as an argument and selects “3” numbers randomly.

Output:

The multiple items have been selected from the sequence.

Example 4: Select Multiple Items From the List Containing Repeated Element Value

The below code is used to select multiple items from the list that contains repeated list value:

Code:

import random
list_value = [45, 55, 45, 45, 85]
output = random.sample(list_value, 3)
print(output)
  • The random module is imported.
  • The “list” named “list_value” is initialized.
  • The “random.sample()” function takes the list and specific integer “3” as an argument and chooses the three random multiple items in the list that contain the same repeated elements.

Output:

The multiple items have been chosen from the given list.

Example 5: Using random.sample() Function to Generate Random Numbers

The “random.sample()” function is used in the below code to generate the random numbers:

Code:

import random
print(random.sample(range(50), 5))
  • The “random” module is imported.
  • The “range()” function accepts the integer as an end argument and creates the range from “0” to “50”.
  • The “random.sample()” function takes the range and specific integer value as an argument to choose 5 values from the sequence. 

Output:

The multiple items have been selected from the given range.

Conclusion

To choose multiple items from any sequence, such as a list, set, dictionary, etc, the “random.sample()” function is used in Python. The “random.sample()” function selects multiple items from the list with and without repeated items. The “dict.items()” function is used along with the “random.sample()” function to select/choose multiple items from the dictionary. 

This post presented a complete overview of how to choose multiple items from the sequence.