EN
Python - access set items
0 points
In this article, we would like to show you how to access set items in Python.
You can't directly access set items by referring to an index or a key. What you can do is to iterate through the set to process its items or check if the set contains a specific value.
In below example, we use simple for loop to iterate through set and print the values.
xxxxxxxxxx
1
my_set = {'A', 'B', 'C'}
2
3
for x in my_set:
4
print(x)
Output:
xxxxxxxxxx
1
A
2
B
3
C
In below, example we use in keyword to check if our set contains B
value.
xxxxxxxxxx
1
my_set = {'A', 'B', 'C'}
2
3
print('B' in my_set) # True
Output:
xxxxxxxxxx
1
True