Sets are non-linear, unordered data structures, which means we can't directly access items using indices like we do with lists. However, there are several ways to retrieve elements from a set. Here are some examples:

We can iterate through the elements in a set using a for-loop to get all the unique set elements. For example:
Code
Output
Creating a Set with Integer Values: 1025 1026 1027 1028 Creating a Set with String Values: Vivek Neeraj Sachin Ashu
Explanation
The above code shows creating sets in Python, one with integer values and another with string values. Then, we used a loop to print the elements of each set.
The sets themselves do not support indexing. To access elements by index, first convert the set into a list and then perform indexing.
Example
Output
{1025, 1026, 1027, 1028, 1030}
1th index: 1026
4th index: 1030
Explanation
In the above example, a collection of whole numbers named data has been created. Subsequently, display the values located at the initial and fourth positions. It is common knowledge that sets do not inherently have positions. Therefore, the set is first changed into a list structure before retrieving elements based on the index value.
To access the last element from a given set, first, we need to convert it into a list data type to make it accessible and then access the last element using the pop() function through the negative indexing:
Example
Output
{1025, 1026, 1027, 1028, 1030}
Last Element: 1030
Explanation
In the given code, we created a set that included integer values and printed that set. Then, we converted the given set into a list and accessed the last element from it using negative indexing.
To retrieve the initial element in the collection, utilize the iter() method followed by invoking the next() function.
Example
Output
{1025, 1026, 1027, 1028, 1030}
First Element: 1025
Explanation
In the code snippet provided above, a set of integer values, named data, has been created and displayed. Subsequently, the initial element is retrieved by utilizing the next() method after converting the collection into an iterator.
Go with the random.sample() function to get a specified number of random elements from the set:
Syntax
Example
Output
{1025, 1026, 1027, 1028, 1030}
Access 2 Random Values: [1028, 1030]
Access 1 Random Value: [1027]
Access 3 Random Values: [1025, 1027, 1028]
Explanation
In the above program, we defined a numerical values set and then used random.sample() function to retrieve random elements from the given set. Then, printed the original set and random subsets.
Sets in Python enable us to store unique objects easily. Accessing elements from a set can be optimized using random.sample() method. This method takes a set as input and returns a random subset of elements from it. One can easily get the random elements from a set by defining the number of elements. Using the set and random.sample() functions provide a straightforward and efficient solution, which helps make Python's data manipulation capabilities more feasible.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India