Select Random Element from Set in Python
Last Updated :
18 Dec, 2023
Selecting a random element from a group of samples is a deterministic task. A computer program can mimic such a simulation of random choices by using a pseudo-random generator. In this article, we will learn how to select a random element from a set in Python.
What is a Set in Python?
A Set is an unordered collection of elements that are iterable, mutable, and has no duplicate elements. The elements within a set can be heterogeneous. Sets utilize a structure known as a hash table to find a given element, making them faster and more efficient in processing than a list. Since sets are unordered, we cannot access items using indexes as we do in lists. The syntax of a set is as follows:
variable_name = {element_one, element_two, element_three}
Where element_one/two/three is any datatype present within Python.
Selecting a Random Element from a Set
Selecting a random element from within several elements could be accomplished by using a pseudo-random generator. For this, the functions in the random library would be used.
Using the choice function
The choice function chooses a random element from a non-empty sequence. The function takes in argument a sequence and returns a random element from it. The following example demonstrates the use of the choice function to select a random element from a predefined set:
Python3
import random
# A set containing elements of different datatype
my_set = {1, "Hello", 38, 44.45, "Apples", False}
print("An element from the set:" , random.choice(list(my_set)))
OutputAn element from the set: 1
Explanation
Firstly a set is defined as containing several elements of different data types. Then the choice function is called, and the set is passed as an argument. The function returns a random element from the set.
Using the randrange function
The randrange function chooses a random item from the given range. The function takes in argument a range (or an endpoint) and returns a random item from the range without including the bounds. The following example demonstrates the use of the randrange function to select a random element from a predefined set:
Python3
import random
# A set containing elements of different datatype
my_set = {1, "Hello", 38, 44.45, "Apples", False}
# Passing the length of the set as an argument to the function
# This produces a random integer between the index 0 to Len-1
element_index = random.randrange(0, len(my_set))
# Obtaining an element from the set based on its index
# Where the index is obtained randomly
element = list(my_set)[element_index]
print("An element from the set:", element)
OutputAn element from the set: 38
Explanation
The same set as the previous example has been defined at first. Then the function randrange is called, and the length of the set is passed as an argument. The function produces a random integer in the given range which is stored in a variable. In the end, the element associated with that index (denoted by the random integer) inside the set is displayed.
Similar Reads
How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem
2 min read
Select random value from a list-Python The goal here is to randomly select a value from a list in Python. For example, given a list [1, 4, 5, 2, 7], we want to retrieve a single randomly chosen element, such as 5. There are several ways to achieve this, each varying in terms of simplicity, efficiency and use case. Let's explore different
2 min read
Retrieve elements from Python Set In this article, we will discuss the different examples of how to retrieve an element from a Python set. Sets are non-linear and unordered data structures so we can't directly access its elements using indices like we do in lists. However there are several ways to do it, here are some examples.Using
3 min read
random.sample() function - Python sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5
2 min read
Remove items from Set - Python We are given a set and our task is to remove specific items from the given set. For example, if we have a = {1, 2, 3, 4, 5} and need to remove 3, the resultant set should be {1, 2, 4, 5}.Using remove()remove() method in Python is used to remove a specific item from a set. If the item is not present
2 min read