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 for
for
loop.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)
Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
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 data
data
. We then print the set elements using the for
for
loop. The output shows that the set elements are accessed using the for
for
loop.
Access the Set using the for loop
In python, we can access the set elements using the for
for
loop. We can use the for
for
loop to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i in data:
print(i)
Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
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 data
data
. We then print the set elements using the for
for
loop. The output shows that the set elements are accessed using the for
for
loop.
Access the Set using the while loop
In python, we can access the set elements using the while
while
loop. We can use the while
while
loop to access the set elements.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
print(data[i])
i += 1
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
print(data[i])
i += 1
Output:
C:\Users\username>python tuple_index.py
a
e
b
d
g
f
c
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 data
data
. We then print the set elements using the while
while
loop. The output shows that the set elements are accessed using the while
while
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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
print(i, j)
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for i, j in enumerate(data):
print(i, j)
Output:
C:\Users\username>python tuple_index.py
0 a
1 e
2 b
3 d
4 g
5 f
6 c
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 data
data
. 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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_list = list(data)
print(data_list[0])
print(data_list[1])
print(data_list[2])
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:
C:\Users\username>python tuple_index.py
a
e
b
C:\Users\username>python tuple_index.py
a
e
b
In this example, we declare a set and assign it to the variable data
data
. 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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data_tuple = tuple(data)
print(data_tuple[0])
print(data_tuple[1])
print(data_tuple[2])
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:
C:\Users\username>python tuple_index.py
a
e
b
C:\Users\username>python tuple_index.py
a
e
b
In this example, we declare a set and assign it to the variable data
data
. 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 in
in
keyword. We can use the in
in
keyword to access the set elements.
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')
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:
C:\Users\username>python tuple_index.py
a is in the set
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 data
data
. We then check if the element a
a
is in the set or not using the in
in
keyword. The output shows that the element a
a
is in the set.
Access element using not in keyword
In python, we can access the set elements using the not in
not in
keyword. We can use the not in
not in
keyword to access the set elements.
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')
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:
C:\Users\username>python tuple_index.py
m is not in the set
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 data
data
. We then check if the element m
m
is in the set or not using the not in
not in
keyword. The output shows that the element m
m
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 for
for
loop, while
while
loop, enumerate()
enumerate()
function, list()
list()
function, tuple()
tuple()
function, in
in
keyword, and not in
not 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