Open In App

Iterate over a set in Python

Last Updated : 26 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Previous Article
Next Article

Similar Reads

Python | Iterate over multiple lists simultaneously
Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step. Iterate over multiple lists at a time For better understanding
4 min read
Iterate over characters of a string in Python
In Python, while operating with String, one can do multiple operations on it. Let's see how to iterate over the characters of a string in Python. Example #1: Using simple iteration and range() C/C++ Code # Python program to iterate over characters of a string # Code #1 string_name = "geeksforgeeks" # Iterate over the string for el
3 min read
Iterate over words of a String in Python
Given a String comprising of many words separated by space, write a Python program to iterate over these words of the string. Examples: Input: str = "GeeksforGeeks is a computer science portal for Geeks" Output: GeeksforGeeks is a computer science portal for Geeks Input: str = "Geeks for Geeks" Output: Geeks for Geeks Method 1: Using split() Using
4 min read
Python - Iterate over Columns in NumPy
Numpy (abbreviation for 'Numerical Python') is a library for performing large-scale mathematical operations in a fast and efficient manner. This article serves to educate you about methods one could use to iterate over columns in an 2D NumPy array. Since a single-dimensional array only consists of linear elements, there doesn't exists a distinguish
3 min read
How to Iterate over Dataframe Groups in Python-Pandas?
In this article, we'll see how we can iterate over the groups in which a data frame is divided. So, let's see different ways to do this task. First, Let's create a data frame: C/C++ Code # import pandas library import pandas as pd # dictionary dict = {'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]} # create a dataframe df = pd.DataFrame(dict) # show
2 min read
How to iterate over OrderedDict in Python?
An OrderedDict is a subclass that preserves the order in which the keys are inserted. The difference between OrderedDict and Dict is that the normal Dict does not keep a track of the way the elements are inserted whereas the OrderedDict remembers the order in which the elements are inserted. Explanation: Input : original_dict = { 'a':1, 'b':2, 'c':
2 min read
How to iterate over files in directory using Python?
Directory also sometimes known as a folder are unit organizational structure in a system’s file system for storing and locating files or more folders. Python as a scripting language provides various methods to iterate over files in a directory. Below are the various approaches by using which one can iterate over files in a directory using python: M
2 min read
Python - How to Iterate over nested dictionary ?
In this article, we will discuss how to iterate over a nested dictionary in Python. Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. Nested dictionary in use: {'Student 1': {'Name': 'Bobby', 'Id': 1, 'Age': 20}, 'Student 2': {'Name': 'ojaswi', 'Id': 2, 'Age':
3 min read
Python - Iterate over Tuples in Dictionary
In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print(dictionary_name[index][i] Example: Python Code #
2 min read
How to Iterate over months between two dates in Python?
In this article, we will discuss how to iterate over months between two dates using Python. We can iterate over months between two dates using timedelta and rrule methods. Method 1: Iteration using timedeltatimedelta() is used for calculating differences in dates and also can be used for date manipulations in Python Example: But using timedelta we
2 min read
Practice Tags :
three90RightbarBannerImg