pantry = {"Flour", "Sugar", "Eggs", "Milk", "Butter"}
Removing duplications
recipe_ingredients_array = ["Flour", "Sugar", "Eggs", "Milk", "Butter", "Sugar"]
unique_ingredients_set = set(recipe_ingredients_array)
Set operations
pantry = {"Flour", "Sugar", "Eggs", "Milk", "Butter"}
recipe = {"Sugar", "Eggs", "Chocolate", "Butter"}
shopping_list = recipe - pantry
Efficient membership testing
time python3 -c "data = list(range(1000000)); membership_checks = [i in data for i in range(10000)]"
I noticed that I underestimate the power of sets. Here are some examples where using a set makes sense:
If you see any of this:
There is a chance that representing as a set can help.
#python