Python – Iterate over elements of a Set

To iterate over elements of a Set in Python, you can use Python For loop. Inside the For loop, during each iteration, we get access to an element from the Set.

Example

In the following program, we initialize a Python Set aSet and iterate over the items of this Set using For loop.

Program

# initialize set
aSet = {'apple', 'cherry', 'banana'}

# iterate over items of a set
for element in aSet:
    print(element)
Try Online

Output

banana
cherry
apple
ADVERTISEMENT

References

Conclusion

In this Python Tutorial, we learned how to iterate over the elements of a given Set using For loop, with the help of example programs.