Python Access Set Element Tutorial

In this section, we will learn how to access Set object’s elements in Python.

Note: we’re assuming you’re already familiar with the Python Set object in general.

Python find Element in Set

As mentioned in the Python Set section, there’s no index number attached to the elements of a set object. Also, set objects don’t have keys like the way we have in dictionaries.

But if we want to access the elements of a Set object, we can use the `for in` loop and traverse through the elements of a Set object.

We can also use the `in` operator to see if a set object has a specific value as part of its elements or not.

Access Set Example

set1 = {"Ellen","Jack", "Omid","Elon"}

set1.add("James")
set1.add("Richard")
set1.add("Jeremy")
set1.add("Ian")
set1.add(True)
set1.add(20)
set1.add(2.23)
set1.add("Kimberly")

for element in set1:
    print(element)

Output:

True

Ian

2.23

Jeremy

Jack

James

Ellen

Richard

Omid

Elon

20

Kimberly
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies