Open In App

Iterate over a set in Python

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements.
There are numerous ways that can be used to iterate over a Set. Some of these ways provide faster time execution as compared to others. Some of these ways include, iterating using for/while loops, comprehensions, iterators and their variations. Let’s see all the different ways we can iterate over a set in Python.
Analysis of each method:
For explaining the working of each way/technique, time per set(randomly generated set) has been calculated for 5-times to get a rough estimate on how much time every technique takes for iterating over a given set. random.seed(21) has been added to each script to fixate over the random numbers that are generated every time the program is executed. Using constant seed helps us to determine which technique is best for a given particular randomly generated set.

Method #1: Iterating over a set using simple for loop.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `# Iterating using for loop``for` `val ``in` `test_set:``    ``print``(val)`

Output:

```k
s
e
g
E```

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``for` `val ``in` `test_set:``        ``_ ``=` `val` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)` `    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output:

```0.06303901899809716
0.06756918999963091
0.06692574200133095
0.067220498000097
0.06748137499744189```

Time complexity: O(n), where n is the number of elements in the set generated.

Auxiliary space: O(n), as the set of random numbers is stored in memory.

Method #2: Iterating over a set using enumerated for loop.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `# Iterating using enumerated for loop``for` `id``,val ``in` `enumerate``(test_set):``    ``print``(``id``, val)`

Output:

```0 E
1 e
2 k
3 g
4 s```

Time complexity: O(n).
Auxiliary space: O(n).

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``for` `id``, val ``in` `enumerate``(test_set):``        ``_ ``=` `val` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```0.1306622320007591
0.13657568199778325
0.13797824799985392
0.1386374360008631
0.1424286179972114```

Method #3: Iterating over a set as indexed list.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `test_list ``=` `list``(test_set)` `# Iterating over a set as a indexed list``for` `id` `in` `range``(``len``(test_list)):``        ``print``(test_list[``id``])`

Output:

```g
k
E
s
e```

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``test_list ``=` `list``(test_set)` `    ``for` `id` `in` `range``(``len``(test_list)):``        ``_ ``=` `test_list[``id``]` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```0.20036015100049553
0.2557020290005312
0.4601482660000329
0.2161413249996258
0.18769703499856405```

Method #4: Iterating over a set using comprehension and list constructor/initializer.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `# Iterating using list-comprehension``com ``=` `list``(val ``for` `val ``in` `test_set)``print``(``*``com)`

Output:

`k s e g E`

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``list``(val ``for` `val ``in` `test_set)` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```0.1662169310002355
0.1783527520019561
0.21661155100082397
0.19131610199838178
0.19931397800246486```

Method #5: Iterating over a set using comprehension.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `# Iterating using list-comprehension``com ``=` `[``print``(val) ``for` `val ``in` `test_set]`

Output:

```e
E
g
s
k```

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``[val ``for` `val ``in` `test_set]` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```0.11386321299869451
0.111869686999853
0.1092844699996931
0.11223735699968529
0.10928539399901638```

Method #6: Iterating over a set using map, lambda and list comprehension

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``[``map``(``lambda` `val: val, test_set)]` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```1.0756000847322866e-05
1.310199877480045e-05
1.269100175704807e-05
1.1588999768719077e-05
1.2522999895736575e-05```

Method #7: Iterating over a set using iterator.

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``for` `val ``in` `iter``(test_set):``        ``_ ``=` `val` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output

```0.0676155920009478
0.07111633900058223
0.06994135700006154
0.0732101009998587
0.08668379899972933```

Method #8: Iterating over a set using iterator and while loop.

Python3

 `# Creating a set using string``test_set ``=` `set``(``"geEks"``)` `iter_gen ``=` `iter``(test_set)` `while` `True``:``    ``try``:``        ``# get the next item``        ``print``(``next``(iter_gen))``        ` `        ``''' do something with element '''``        ` `    ``except` `StopIteration:``        ``# if StopIteration is raised,``        ``# break from loop``        ``break`

Output:

```E
s
e
k
g```

Analysis:

Python3

 `# importing libraries``from` `timeit ``import` `default_timer as timer``import` `itertools``import` `random` `# Function under evaluation``def` `test_func(test_set):` `    ``iter_gen ``=` `iter``(test_set)``    ``while` `True``:``        ``try``:``            ``# get the next item``            ``next``(iter_gen)``            ``# do something with element``        ``except` `StopIteration:``            ``# if StopIteration is raised, break from loop``            ``break` `# Driver function``if` `__name__ ``=``=` `'__main__'``:` `    ``random.seed(``21``)``    ``for` `_ ``in` `range``(``5``):``        ``test_set ``=` `set``()` `        ``# generating a set of random numbers``        ``for` `el ``in` `range``(``int``(``1e6``)):``            ``el ``=` `random.random()``            ``test_set.add(el)` `        ``start ``=` `timer()``        ``test_func(test_set)``        ``end ``=` `timer()` `        ``print``(``str``(end ``-` `start))`

Output:

```0.2136418699992646
0.1952157889973023
0.4234208280031453
0.255840524998348
0.24712910099697183```

Conclusion:
Among all the looping techniques, simple for loop iteration and looping over iterators works best, while comparing all the techniques, using map with lambda over set or iterator of set works best giving a performance of a million set iterations under 10 milliseconds. It is quite noticeable that above examples only have single access of set components per iteration, whereas if we increase the number of times a set component is accessed per iteration, it may change the time taken per iteration.
Note: Values mentioned above in the example output are bound to vary. The reason behind the variation of time consumption is machine dependency of processing power of individual’s system processor.