6 Easy Ways to Iterate Through Set in Python

A set in Python is an unordered collection of items. Because the objects in a set are not ordered, indexes cannot be used to access them. In Python, sets come in handy when conducting mathematical calculations. The different types of operations present in a set are Union, intersection, complement difference, etc. In this tutorial, we will learn about how to iterate through set in Python.

Contents

6 Methods to iterate through set in Python

As mentioned above, sets are an unordered collection, and so the items cannot be accessed through indexing. If we want to access the set items, we will have to iterate with the help of loop statements. Some of the ways to iterate through set in python are:

1. Using for loop statement
2. Enumerating over a set
3. Using iter with for loop
4. Converting the set to a list
5. Using comprehension
6. Iterating over two sets simultaneously using zip()

1. Using for loop statement

The simplest way of iterating through a set is using the for loop statement. Let us consider a set named ‘my_set’ containing names of five different cities.

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
```

Now, if we want to access each set element, then we can do that by printing the set element wise using for loop.

```for item in my_set:
print(item)
```

The output of the code is:

```london
seattle
new york
chicago
sydney```

The entire code is:

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
for item in my_set:
print(item)
```

2. Enumerating over a set

We can also use enumerated for loop for iterating over python sets. When we use enumerate() method, we receive a counter along with the iterable item.

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
for counter, item in enumerate(my_set):
print(item)
```

As the output, we will print the iterable item.

```london
seattle
new york
chicago
sydney```

You can also print the counter along with the iterable item. For that, we shall print the variable ‘counter‘ along with ‘item’.

The code is:

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
for counter, item in enumerate(my_set):
print(counter,":",item)
```

The output of the code is:

```0 : london
1 : seattle
2 : new york
3 : chicago
4 : sydney```

3. Using iter with for loop

We can use the iter() method as well for iterating through a python set. The iter() method will return an iterator. Using that iterator, we can iterate over a given object. We shall use the iter() method with a for loop.

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
for item in iter(my_set):
print(item)
```

The output is:

```london
seattle
new york
chicago
sydney```

4. Converting the set to a list

We can also convert a set explicitly to a list in order to iterate over it. We shall take the same list as before:

```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
```

Now, we shall convert the ‘my_set’ set to a list using list() and store that list into a variable named ‘my_list’.

```my_list = list(my_set)
```

Then, we can iterate over that list using indexing.

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

The output is:

```london
seattle
new york
chicago
sydney```

Instead of a for loop statement, we can also use a while loop. We take a counter variable ‘i’, which is initially equal to zero. Then, we can check in the while loop condition that the counter ‘i’ should be less than the length of ‘my_list’. We will print each list item using indexing and increment the variable ‘i’ every time.

```i = 0
while i < len(my_list):
print(my_list[i])
i = i + 1
```

Output:

```london
seattle
new york
chicago
sydney```

5. Using comprehension

Comprehension in python is a compact piece of code used to generate new sequences from already existing sequences. Comprehension consists of three main parts – the expression to be printed, the iterable item, and the list, which is the sequence. Here, we shall generate a sequence of items from the existing set my_set.

The syntax for list comprehension is:

`[expression for item in list]`
```my_set = {'london', 'new york', 'seattle', 'sydney','chicago'}
val = [print(item) for item in my_set]
```

The output of the code is:

```london
seattle
new york
chicago
sydney```

6. Iterating over two sets simultaneously using zip()

We can also iterate over two sets in python using the zip() function. The zip function returns a zip object. With that, we can iterate over two or more iterables simultaneously. This saves a lot of computation time because multiple sequences are iterated simultaneously.

We shall take the following two sets – ‘set1’ and ‘set2’.

```set1 = {10, 20, 30, 40, 50}
set2 = {100, 200, 300, 400, 500}
```

Using the zip() function, we shall create two iterables for each set – ‘i1’ and ‘i2’. Then, we shall print i1 and i2.

```for (i1, i2) in zip(set1, set2):
print(i1, i2)
```

The output is:

```40 100
10 200
50 300
20 400
30 500```

Note here that since it is a set, the iterables printed would not be in order.

This wraps up different ways to iterate through set in python. If you have any questions in your mind or any thoughts to share, leave them below in the comments.

Until next time, Keep Learning!

Subscribe
Notify of