Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Technical Interview Prep: Python Basics Basic Python Down The Rabbit Hole

Technical Interview Prep: Python Basics > Down The Rabbit Hole

Bummer: It should have been ['Australian Shepherd', 'Chihuahua', 'Russian Blue']. Instead, it was []

nested.py
# {
#     'name': 'Javier Hernandez',
#     'pets': [
#         {
#             'name': 'Kitty',
#             'breed': 'American Shorthair'
#         },
#         {
#             'name': 'Buzz',
#             'breed': 'Pitbull'
#         }
#     ],
#     'classes': ('Math', 'Science', 'Art')
# }
def breeds(data):
    list1 = []
    for key, value in data.items():
        if key == 'breed':
            list1.append(value)
    return list1

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Hey Trenton Spears, since the breed information is deeper in side the data, you will need to parse deeper into the structure. Sometimes a problem solution does not have to be generic for all input data, it can be very specific and work only for a very specific data form.

Try the following flow:

  • set the embedded pets list to a variable
  • loop through the list, adding the breeds to your list1
  • return the list1

Post back if you need more help. Good luck!!

I have no idea man. I've been stuck on this for about two weeks binging tutorial videos on YouTube, Treehouse, and online forums of all sorts and I'm dead on my last three exercises. Just being totally honest I'm about ready to hang it up before my next billing cycle.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

I can try to walk you through it. Do you have a local environment set up to exercise code?

I'll open up VS Code right now

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Let’s solve it without a function first.

Copy in the example dictionary (above) and assign it to a variable:

data = {
    'name': 'Javier Hernandez',
    'pets': [
        {
            'name': 'Kitty',
            'breed': 'American Shorthair'
        },
        {
            'name': 'Buzz',
            'breed': 'Pitbull'
        }
    ],
    'classes': ('Math', 'Science', 'Art')
}

set pets equal to the β€œpets” item in the dict data:

pets = data[β€œpets”]

pets is now a list of dictionaries. Loop through pets extracting the β€œbreed” information from each dict:

breeds = []
for dct in pets:
    breeds.append(dct[β€œbreed”])
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The final step is to put the code in a function. The code above should work under the function β€œsignature”, that is, the β€œdef” line you have above.

If I set this up in my editor as instructed then should I still be getting a TypeError?

data = {
    'name': 'Javier Hernandez',
    'pets': [
        {
            'name': 'Kitty',
            'breed': 'American Shorthair'
        },
        {
            'name': 'Buzz',
            'breed': 'Pitbull'
        }
    ],
    'classes': ('Math', 'Science', 'Art')
}


def breeds(data):

    pets = data["pets"]
    breeds = []
    for dct in pets:
        breeds.append(dct("breed"))

    return breeds
File "c:/Users/trent/Practice.py", line 28, in <module>
    print(breeds(data))
  File "c:/Users/trent/Practice.py", line 23, in breeds
    breeds.append(dct("breed"))
TypeError: 'dict' object is not callable
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Sorry. My typo. Should be dct[β€œbreeds”] using square brackets.

Got it! Thanks Chris, I really appreciate your time and assistance man.

def breeds(data):

    pets = data["pets"]
    breeds = []
    for dct in pets:
        breeds.append(dct["breed"])

    return breeds