In Python, iterating through sets can be done in many ways. The easiest way to iterate through a set in Python is with a loop.

set = {1,2,3}

for x in set:
    print x

#Output:
1
2
3

You can also use set comprehension to iterate through a set.

set = {1,2,3}

output = [print(x) for x in set]

#Output:
1
2
3

Another method to iterate through a set is with the enumerate() method in Python.

set = {1,2,3}

for idx, x in enumerate(set):
  print("index ": idx + ", set_value: " + x)

#Output:
index: 0, set_value: 1
index: 1, set_value: 2
index: 2, set_value: 3

You can also convert a set to a list to be able to use indexing to iterate over a set.

set = {1,2,3}
list_from_set = list(set)

for i in range(0,len(list_from_set)):
    print(list_from_set[i])

#Output:
1
2
3

In Python, sets are unordered collections of items. Because the objects in a set are not ordered, unfortunately, we cannot use indexing to access the objects.

Iteration is one of the fundamental operations in programming, and the ability to loop over objects easily is very important.

Looping over sets is easy in Python and is similar to iterating through other objects in Python. The easiest way to loop over a set in Python is with a standard for loop.

Even though we cannot access the items of a set by index directly, we can still iterate through the set with a loop.

Below is an example of how to iterate through a set in Python using a for loop.

set = {1,2,3}

for x in set:
    print x

#Output:
1
2
3

Using Set Comprehension to Iterate through Set in Python

Just like with lists, we can use comprehension to iterate through a set in Python. Comprehension in Python allow us to generate new collections from already existing collections with one line of code.

With comprehension, we can iterate over a set easily.

Below is an example in Python of using comprehension to iterate over a set.

set = {1,2,3}

output = [print(x) for x in set]

#Output:
1
2
3

Using Enumeration to Iterate through Set in Python

The Python enumerate() function allows us to iterate through sets, and also allows us to get the index of each item in a set.

As we know, sets do not allow us to access their items by index, but when we call the enumerate() function, we get the index.

After using the enumerate() function, we can then loop over the items and get the index and value of each element of our set.

Below is how to iterate through a set using the Python enumerate() function.

set = {1,2,3}

for idx, x in enumerate(set):
  print("index ": idx + ", set_value: " + x)

#Output:
index: 0, set_value: 1
index: 1, set_value: 2
index: 2, set_value: 3

Converting a Set to List for Iterating in Python

One last example of how to loop over a set is to convert the set to a list.

If you don’t like the previous example with the enumerate() function, but want to access the set items by index, you can convert the set to a list and then use a for loop.

Below is an example in Python of how to convert a set to a list and iterate over the list using Python.

set = {1,2,3}
list_from_set = list(set)

for i in range(0,len(list_from_set)):
    print(list_from_set[i])

#Output:
1
2
3

Hopefully this article has been helpful for you to learn how to iterate through sets in Python.

Categorized in:

Python,

Last Update: February 26, 2024