xxxxxxxxxx
my_set = {1, 2, 3, 4, 5}
# Method 1: Using a for loop
for item in my_set:
print(item)
# Method 2: Using the built-in iter() function
set_iterator = iter(my_set)
while True:
try:
item = next(set_iterator)
print(item)
except StopIteration:
break
# Method 3: Converting the set to a list and iterating over it
for item in list(my_set):
print(item)
xxxxxxxxxx
cuisine = {"american", "italian", "chinese"}
for food in cuisine:
print(food)
xxxxxxxxxx
dishes = {"Parotta","Chappathi","Beef Curry"}
for dish in dishes:
print(dish)
#Parotta
#Chappathi
#Beef Curry
#To make your code look more "Pythonic" :)
dishes = {"Parotta","Chappathi","Beef Curry"}
for key,dish in enumerate(dishes):
print(dish)
#Parotta
#Chappathi
#Beef Curry
xxxxxxxxxx
# Creating a set using string
test_set = set("geEks")
# Iterating using for loop
for val in test_set:
print(val)