Iterating Over Sets

Tags: Iteration, Set

Write a Python program that iterates over a set and prints each element.

Example 1:

Input: {1, 2, 3, 4, 5} 
Output: 1 2 3 4 5 (The order of output can vary as set elements have no specific order)

Example 2:

Input: {5, 7, 8, 9, 10} 
Output: 5 7 8 9 10 (The order of output can vary as set elements have no specific order)

You can iterate over a set using a simple for loop.

def iterate_set(s):
    for i in s:
    print(i)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!