Skip to content

Access the Set

Access the Set

In python, we can not access the set elements using the index number. Because the set is an unordered collection of items. So, we can not access the set elements using the index number. But we can access the set elements using the forfor loop.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In this example, we declare a set and assign it to the variable datadata. We then print the set elements using the forfor loop. The output shows that the set elements are accessed using the forfor loop.

Access the Set using the for loop

In python, we can access the set elements using the forfor loop. We can use the forfor loop to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
    print(i)

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In this example, we declare a set and assign it to the variable datadata. We then print the set elements using the forfor loop. The output shows that the set elements are accessed using the forfor loop.

Access the Set using the while loop

In python, we can access the set elements using the whilewhile loop. We can use the whilewhile loop to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
    print(data[i])
    i += 1
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
    print(data[i])
    i += 1

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
command
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c

In this example, we declare a set and assign it to the variable datadata. We then print the set elements using the whilewhile loop. The output shows that the set elements are accessed using the whilewhile loop.

Access the Set using the enumerate() function

In python, we can access the set elements using the enumerate()enumerate() function. We can use the enumerate()enumerate() function to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
    print(i, j)
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
    print(i, j)

Output:

command
C:\Users\username>python tuple_index.py
0 a
1 e
2 b
3 d
4 g
5 f
6 c
command
C:\Users\username>python tuple_index.py
0 a
1 e
2 b
3 d
4 g
5 f
6 c

In this example, we declare a set and assign it to the variable datadata. We then print the set elements using the enumerate()enumerate() function. The output shows that the set elements are accessed using the enumerate()enumerate() function.

Access the Set using the list() function

In python, we can access the set elements using the list()list() function. We can use the list()list() function to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_list = list(data)
print(data_list[0])
print(data_list[1])
print(data_list[2])
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_list = list(data)
print(data_list[0])
print(data_list[1])
print(data_list[2])

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
command
C:\Users\username>python tuple_index.py
a
e
b

In this example, we declare a set and assign it to the variable datadata. We then convert the set into a list using the list()list() function. We then print the list elements using the index number. The output shows that the list elements are accessed using the index number.

Access the Set using the tuple() function

In python, we can access the set elements using the tuple()tuple() function. We can use the tuple()tuple() function to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_tuple = tuple(data)
print(data_tuple[0])
print(data_tuple[1])
print(data_tuple[2])
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_tuple = tuple(data)
print(data_tuple[0])
print(data_tuple[1])
print(data_tuple[2])

Output:

command
C:\Users\username>python tuple_index.py
a
e
b
command
C:\Users\username>python tuple_index.py
a
e
b

In this example, we declare a set and assign it to the variable datadata. We then convert the set into a tuple using the tuple()tuple() function. We then print the tuple elements using the index number. The output shows that the tuple elements are accessed using the index number.

Access element using in keyword

In python, we can access the set elements using the inin keyword. We can use the inin keyword to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'a' in data:
    print('a is in the set')
else:
    print('a is not in the set')
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'a' in data:
    print('a is in the set')
else:
    print('a is not in the set')

Output:

command
C:\Users\username>python tuple_index.py
a is in the set
command
C:\Users\username>python tuple_index.py
a is in the set

In this example, we declare a set and assign it to the variable datadata. We then check if the element aa is in the set or not using the inin keyword. The output shows that the element aa is in the set.

Access element using not in keyword

In python, we can access the set elements using the not innot in keyword. We can use the not innot in keyword to access the set elements.

tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'm' not in data:
    print('m is not in the set')
else:
    print('m is in the set')
tuple_index.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'm' not in data:
    print('m is not in the set')
else:
    print('m is in the set')

Output:

command
C:\Users\username>python tuple_index.py
m is not in the set
command
C:\Users\username>python tuple_index.py
m is not in the set

In this example, we declare a set and assign it to the variable datadata. We then check if the element mm is in the set or not using the not innot in keyword. The output shows that the element mm is not in the set.

Conclusion

In this tutorial, we learned how to access the set elements in python. We learn how to access the set elements using the forfor loop, whilewhile loop, enumerate()enumerate() function, list()list() function, tuple()tuple() function, inin keyword, and not innot in keyword. For more information, visit the official website of the python set. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did