12.1 Access Set Items

By | October 2, 2021

Access Items

You cannot access items in a set by referring to an index or a key.

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 keyword.

Example

Loop through the set, and print the values:
thisset = {“apple”, “banana”, “cherry”}
for x in thisset:
print(x)

Output:
apple
cherry
banana

Example

Check if “banana” is present in the set:
thisset = {“apple”, “banana”, “cherry”}
print(“banana” in thisset)

Output:
True

Change Items

Once a set is created, you cannot change its items, but you can add new items.

Leave a Reply

Your email address will not be published. Required fields are marked *