To iterate over a set in Python, you can use a for loop.
In this example,
string_set
containing various strings.string_set
.string_set = {'Apple', 'Banana', 'Orange'}
for string in string_set:
print(string)
Apple Banana Orange
In this example,
int_set
containing various integer values.int_set
.int_set = {1, 2, 3, 4, 5}
for number in int_set:
print(number * 2)
2 4 6 8 10
In this tutorial, we learned How to Iterate Over a Set in Python language with well detailed examples.