Python

Display nested dictionary content sorted by key in Python

Given a nested dictionary like this:

dog_breeds = {
    'Labrador Retriever': {'life_span': 14, 'male_weight': '36 Kg', 'female_weight': '32 Kg'},
    'Beagle': {'life_span': 15, 'male_weight': '11 Kg', 'female_weight': '10 Kg'},
    'German Shepherd': {'life_span': 13, 'male_weight': '40 Kg', 'female_weight': '32 Kg'},
    'Jack Russell Terrier': {'life_span': 16, 'male_weight': '8 Kg', 'female_weight': '8 Kg'},
    'Rottweiler': {'life_span': 10, 'male_weight': '60 Kg', 'female_weight': '48 Kg'}
}

here’s a way to display its content sorted by key:

Map a string in Python with enumerate

Problem: create a map of the letters and indices in a string.

My first approach was to loop over the string using range(len(s)), and checking if the letter exists in the map before adding it:

Newer Posts