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.
1. Iterate through set
In below example, we use simple for loop to iterate through set and print the values.
my_set = {'A', 'B', 'C'}
for x in my_set:
print(x)
Output:
A
B
C
2. Check if set contains specific value
In below, example we use in keyword to check if our set contains B
value.
my_set = {'A', 'B', 'C'}
print('B' in my_set) # True
Output:
True