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: Uh oh, I didn't get the correct value returned. It should have been ['Australian Shepherd', 'Chihuahua', 'Russia

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  

2 Answers

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hi there! When in doubt, always give your work a try in workspaces or in an IDE of your choice. For instance, I tested your function in Visual Studio Code by seeing what key, value was being accessed:

data_struc = {
    'name': 'Javier Hernandez',
    'pets': [
        {
            'name': 'Kitty',
            'breed': 'American Shorthair'
        },
        {
            'name': 'Buzz',
            'breed': 'Pitbull'
        }
    ],
    'classes': ('Math', 'Science', 'Art')
}
# enter your code below
def breeds(data):
    list1 = []
    for key, value in data.items():
        # check what you're accessing
        print(key, value)
        if key == 'breed':
            list1.append(value)
    return list1  

print(breeds(data_struc))

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

Now you can see that you're accessing the initial dictionary key values pairs which doesn't include 'breed' so you will always return an empty list.

Hey, Megan Amendola thanks for reaching out, unfortunately, I'm still stuck. Is there either another way you could break down your guidance or, a video section within a particular course you maybe could recommend?

Megan Amendola
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hey Trenton Spears ! Think about the file structure. How do you actually get to the breed? Right now you aren't "deep" enough in the file structure to actually access the breed. The first structure is a dictionary with the keys: names, pets, and classes. Then you have to go deeper. The breed is inside of pets, so first, you need to access pets data['pets']. Now looking at pets, it's a list with dictionaries inside of it. You need to loop through the list and grab the breed from each dictionary inside of the pets list. Hope this helps!

Bummer: Uh oh, I didn't get the correct value returned. It should have been ['Australian Shepherd', 'Chihuahua', 'Russian Blue']. Instead, it was Pitbull

def breeds(data):
    pets = {
        'name': 'Kitty',
        'breed': 'American Shorthair',
        'name': 'Buzz',
        'breed': 'Pitbull'
    }

    x = pets['breed']
    return x

    for x in pets.key():
        return x

Does this have something to do with the structuring of my dictionary list?

Megan Amendola
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

The data structure will be passed into the function and will look like the example in the code challenge, so you shouldn't be creating your own data structure. Also, the first return statement will stop your function so the for loop never runs.

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

Your first take was very close, you just need to continue down the data structure to get to the breeds. I would suggest opening a workspace and test out accessing a breed using print statements with the example data structure above.

For instance, accessing the pets would be data['pets'] which would return a list with dictionaries inside of it. To access the first dictionary I would need to use list notation because it's inside of a list data['pets'][0] which would return the first dictionary inside of list pets. Then you need to access the breed which is inside a dictionary data['pets'][0]['breed'].

Finally got it..

def breeds(data):

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

    return breeds