How to Select a Random Item From a List and Set? | Python random.choice()

In Python, the inbuilt “random” module is used to generate the random numbers or select random numbers from the given list, range, etc. To select a random item from the given list the “random.choice()” function is used in Python.

This blog post will cover various examples of using the “random.choice()” function to select a random element from a list and set. 

How to Select Random Items From the List Using random.choice() Function?

The “random.choice()” function of the random module is used to select the random elements from the given list. The following examples show the random selection of items from single or multiple lists:

Example 1: Select Single Item From a List

The given below code is used to select a random element from the list:

Code: 

import random
list_Value = ['Joseph', 'Henry', 'Kane', 'Anna']
output = random.choice(list_Value)
print(output)

The “random.choice()” takes the input list as an argument and returns a random item from the given list.

Output:

The random item named “Joseph” has been retrieved by the random.choice() function.

Example 2: Select Multiple Items From List

The “random.choices()” function is used to select more than one item from the list. The given below code shows how to select multiple items from the input list:

Note: The “random.sample()” function is also used when we want to select random items from the input list without any duplication items.

Code: 

import random
list_Value = ['Joseph', 'Henry', 'Kane', 'Anna', 'Lily']
output = random.choices(list_Value, k=3)
print(output)

The “random.choices()” function accepts the list and number of random items to be generated as an argument and returns the random items from the given list.

Output:

The multiple random items from the given list have been successfully returned.

Example 3: Select Random Item From Multiple Lists

The random items from multiple lists can also be selected using the “random.choice()” and “random.choices()” methods. Here is an example code:

Code:

import random
list_value1 = ['Joseph', 'Henry', 'Kane', 'Anna', 'Lily']
list_value2 = ['Joseph', 'Alex', 'John', 'Jonathon', 'Mike']
lists = list_value1, list_value2
x = map(len, lists)
output = random.choice(random.choices(lists, weights=x)[0])
print(output)

In the above code:

  • The multiple list values named “list_value1” and “list_value2” are initialized in the program.
  • The tuple value named “lists” is created in the program.
  • The “map()” function is used to map the object that applies the specified “len” function to each list in “lists”. The function produces a map object with the lengths of each list.
  • The “random.choices()” function accepts the tuple lists and “weights” parameter as an argument and returns a random item from a list based on the weights provided. 
  • The “weights” parameter indicates the weights of each item in the list.
  • The “random.choices” method is used to select a random list, and the “random.choice” method is used to select a random name from the selected list.

Output:

The random item from the given list has been selected successfully.

Example 4: Select Random Item Along With Index Position

The below code uses the “random.randrange()” function along with the list to select a random item with index position:

Code:

import random
list_value = ['Joseph', 'Henry', 'Kane', 'Anna', 'Lily']
value = random.randrange(len(list_value))
print(list_value[value], value)

The “random.randrange()” function is used to take the length of the list as a range and randomly select items from the given range.

Output:

The random item from the given list has been selected.

How to Select Random Items From Set Using random.choice() Function?

The “random.choice()” function is used to return the random items from the given set. The following examples show how to select a random item from the set:

Example 1: Select Random Item From Set

The below code is used to select a random item from the set:

Code: 

import random
set_value = {'Joseph', 'Henry', 'Kane', 'Anna', 'Lily'}
output = random.choice(tuple(set_value))
print(output)

In the above code:

  • The set value is converted into a tuple because the set value is not sub-scriptable in Python.
  • The “random.choice()” method is used to select the random value from the tuple.

Output: 

The above output shows the random item value.

Example 2: Select Multiple Items From a Set

The below code is used to select multiple items from a set:

Code:

import random
set_value = {'Joseph', 'Henry', 'Kane', 'Anna', 'Lily'}
output = random.choices(tuple(set_value), k=3)
print(output)

The “random.choices()” function is used to select multiple items from a set by accepting the number of random items “k=3” as an argument.

Output:

Multiple random items from the given set have been selected.

Conclusion

The “random.choice()” and “random.choices()” functions are used to select random items from single or multiple lists or sets. The “random.choices()” function accepts the number of elements to be returned as an argument and returns the multiple random values from the given list. The “random.choice()” and “random.choices()” can be used combinedly to select the random items from the multiple lists. This tutorial delivered an in-depth overview of the Python “random.choice()” method using various examples.