Python Access Set Items
Access Set Items
You cannot access items in a set by referring to an index, since sets are unordered the items has no index.
But you can loop through the set items using a for
loop, or ask if a specified value is present in a set, by using the
in
keyword.
Example
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Try it Yourself »
for x in thisset:
print(x)
Example
Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
Try it Yourself »
print("banana" in thisset)
Related Pages
Python Sets Tutorial Set Add Set Items Loop Set Items Check if Set Item Exists Set Length Remove Set Items Join Two Sets