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

</>
Copy
# initialize set
aSet = {'apple', 'cherry', 'banana'}

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

Output

banana
cherry
apple

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.