Iterating Over Sets
Part of the Logic & Flow section of Coddy's Python journey — lesson 37 of 78.
Iterating over a set is similar to iterating over a list or a dictionary. You can use a for loop to go through each element in a set. However, it's important to remember that sets are unordered, so the elements will not be processed in a specific sequence.
Here's an example of iterating over a set:
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element)Output:
1
2
3
4
5In this example, the for loop iterates over each element in my_set and prints it. The order of the elements in the output may vary because sets do not maintain a specific order.
If you need to access the elements of a set in a specific order, you can convert the set to a list and then iterate over the sorted list:
my_set = {5, 2, 8, 1, 3}
sorted_list = sorted(list(my_set))
for element in sorted_list:
print(element)Output:
1
2
3
5
8In this example, we first convert my_set to a list using list(my_set), then sort the list using sorted(). The for loop then iterates over the sorted list, printing each element in ascending order.
Challenge
EasyCreate a function named iterate_and_filter_set that takes a set input_set as an argument. The function should iterate over the elements in the set and filter out numbers that are greater than 10. The function should return a new set containing only the numbers that are less than or equal to 10.
Cheat sheet
Use a for loop to iterate over a set. Remember that sets are unordered:
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element)To iterate over a set in a specific order, convert it to a sorted list:
my_set = {5, 2, 8, 1, 3}
sorted_list = sorted(list(my_set))
for element in sorted_list:
print(element)Try it yourself
def iterate_and_filter_set(input_set):
# Write code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter