Friday, December 27, 2024
Google search engine
HomeLanguagesRetrieve elements from Python Set

Retrieve elements from Python Set

In this article, we will discuss the different examples of how to retrieve an element from a Python set in Python.

Examples to Retrieve elements from Python Set

Retrieve all elements from a set without a duplicate value

By iterating the elements in a set using for loop we can get all the unique set elements.

Python3




# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076, 7076}
 
print("create a set with integer elements:")
 
# display set elements using for loop
for i in data:
    print(i)
 
print("\ncreate a set with string elements:")
 
# create a set with string elements
data1 = {"sravan", "harsha", "jyothika", "harsha"}
 
# display set elements using for loop
for i in data1:
    print(i)


Output:

create a set with integer elements:
7072
7074
7076
7058
7059

create a set with string elements:
jyothika
sravan
harsha

Access set elements by index

In a set we can not perform indexing, first, we have to convert that set into a list and then perform the indexing. So we are using the Python list() function to convert the set into the list.

Python3




# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
print(data)
 
# retrieve 1 st element
print("0th index: ", list(data)[0])
 
# retrieve 4 th  element
print("4th index: ", list(data)[3])


Output:

{7072, 7074, 7076, 7058, 7059}
0th index:  7072
4th index:  7058

Get the Last Element of a Set in Python

We can convert it into a list and then access the last element by using the pop() function. This will get the last element or we can also use index = -1 to get the last element.

Python3




# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
print(data)
 
# retrieve last element
print(list(data)[-1])
 
# retrieve last element
print(list(data).pop())


Output:

{7072, 7074, 7076, 7058, 7059}
7059
7059

Get the First Element of a Set in Python

We can access the first item in the set by using the iter() function, we have to apply the next() to it to get the first element. We can also use the first() method from the iteration_utilities module, Which will return the first element.

Python3




# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
print(data)
 
# retrieve first element
print(next(iter(data)))


Output:

{7072, 7074, 7076, 7058, 7059}
7072

Retrieve Random elements from Python Set

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.

Python3




# import random module
import random
 
# create a set with integer elements
data = {7058, 7059, 7072, 7074, 7076}
print(data)
 
# retrieve 2 random elements
print("retrieve 2 random number:", random.sample(data, 2))
 
# retrieve 1 random element
print("retrieve 2 random number:", random.sample(data, 1))
 
# retrieve 4 random elements
print("retrieve 2 random number:", random.sample(data, 4))


Output:

{7072, 7074, 7076, 7058, 7059}
retrieve 2 random number: [7074, 7072]
retrieve 2 random number: [7058]
retrieve 2 random number: [7059, 7058, 7072, 7076]

RELATED ARTICLES

Most Popular

Recent Comments