Kodewick
Start learning →
Basic←→0 / 80

Wardrobe by color

#035BasicDictsLists4 min

Tidying the wardrobe, you note each piece as (item, color). Write by_color(clothes) to return {color: [items of that color]}, with colors and items in the order they came. No clothes gives {}.

Examples
by_color([("shirt", "blue"), ("socks", "red"), ("jeans", "blue")])→{'blue': ['shirt', 'jeans'], 'red': ['socks']}
by_color([])→{}
main.py
def by_color(clothes):
    groups = {}
    for item, color in clothes:
        # your code here: give the color an empty list if it has none yet, then add the item
        pass
    return groups
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.