DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 50 of 77
←PreviousPrevNextNext→

Loop Sets

Looping Through Sets

You can iterate through a set using a for loop. Since sets are unordered collections, the order of iteration is not guaranteed and may vary between runs.

Example
# Basic looping through a set
fruits = {"apple", "banana", "cherry"}

print("Fruits in set:")
for fruit in fruits:
    print(f"- {fruit}")
Output
Fruits in set:
- apple
- banana
- cherry

Looping with enumerate

If you need an index while looping, use enumerate(). The index is assigned in the order of iteration, but remember that set order is not predictable.

Example
# Looping with index using enumerate
colors = {"red", "green", "blue"}

for index, color in enumerate(colors):
    print(f"Color {index}: {color}")
Output
Color 0: red
Color 1: green
Color 2: blue

Set Comprehension

Set comprehensions allow building sets concisely, similar to list comprehensions, and support expressions and conditions.

Example
# Set comprehension example
numbers = {1, 2, 3, 4, 5}

# Create a new set with squared values
squared = {x**2 for x in numbers}
print(squared)

# Create a set with only even numbers
even_numbers = {x for x in numbers if x % 2 == 0}
print(even_numbers)
Output
{1, 4, 9, 16, 25}
{2, 4}

While Loop with Sets

You can also use a while loop with sets by repeatedly removing elements. This is less common but can be useful in certain scenarios. Use a copy if you want to preserve the original set.

Example
# Using while loop with sets
numbers = {1, 2, 3, 4, 5}
temp_set = numbers.copy()

while temp_set:
    item = temp_set.pop()
    print(f"Removed: {item}")

print(f"Original set unchanged: {numbers}")
Output
Removed: 1
Removed: 2
Removed: 3
Removed: 4
Removed: 5
Original set unchanged: {1, 2, 3, 4, 5}

Nested Loops with Sets

Nested loops allow comparing elements across multiple sets. While you can use built-in methods like intersection() for efficiency, nested loops can be used for custom comparisons.

Example
# Comparing elements between two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

print("Common elements:")
for item1 in set1:
    for item2 in set2:
        if item1 == item2:
            print(f"- {item1}")
Output
Common elements:
- 3
- 4
Test your knowledge: Loop Sets
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 50 of 77
←PreviousPrevNextNext→