Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,464 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 67,464 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 67,464 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 67,464 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 67,464 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