Open In App

Retrieve elements from Python Set

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 indexing

Sets do not support indexing, so to access elements by index, convert the set into a list using the list() function. The last element can also be retrieved using the pop() function.

Python
s = {11, 73, -22, "Geeks", '@'}

# retrieve 1 st element
print("0th index: ", list(s)[0])

# retrieve 4 th  element
print("4th index: ", list(s)[3])

# retrieve last element using pop()
print(list(s).pop())

Output
0th index:  Geeks
4th index:  11
@

Explanation: list(s) converts the set ‘s’ into a list and then it can be easily accessed using indexing like a normal list.

Note:

  1. The output may not appear in the same sequence as the set because sets are unordered. Additionally, each execution may produce a different order of elements.
  2. pop() operation removes an element from the converted list, not the original set as the operation is performed on the list not the set itself.

Using for loop

By using a for loop to iterate through a set, we can access all its elements one by one.

Python
s = {11, 73, -22, "Geeks", '@'}

for i in s:
    print(i)

Output
@
Geeks
73
-22
11

Explanation: By using a for loop, we can iterate through all the elements of a set. However, due to its unordered nature, the elements are retrieved in a random order.

Using iter() function

We can use the iter() function to create an iterator of the set and then call next() on it. However, since sets are unordered the element retrieved may vary.

Python
s = {11, 73, -22, "Geeks", '@'}

# create an iterator of the set
iterator = iter(s)

# retrieve first element
print(next(iterator))

# retrieve second element 
print(next(iterator))

Output
@
Geeks

Explanation: iter(s) creates an iterator for the set ‘s’, allowing sequential access to its elements. next(iterator) function retrieves elements one by one in an arbitrary order due to the unordered nature of sets.

Retrieve Random elements

We can get n number of random elements from a set using sample() function. This is available in a random module and this will return a list of sample elements.

Python
# import random module
import random

s = {11, 73, -22, "Geeks", '@'}

# retrieve 2 random elements
print("retrieve 2 random number:", random.sample(list(s), 2))

# retrieve 4 random elements
print("retrieve 4 random number:", random.sample(list(s), 4))

Output
retrieve 2 random number: [-22, 73]
retrieve 4 random number: ['@', 11, -22, 'Geeks']

Explanation: list(s) converts ‘s’ into a list since random.sample() requires a sequence. It then selects a specified number of unique random elements (2 and 4 in this case) from the list without replacement, ensuring no duplicates in each selection



Next Article
Article Tags :
Practice Tags :

Similar Reads