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 trialTrenton Spears
Python Development Techdegree Graduate 16,969 PointsTechnical Interview Prep: Python Basics > Down The Rabbit Hole
Bummer: It should have been ['Australian Shepherd', 'Chihuahua', 'Russian Blue']. Instead, it was []
# {
# '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
Treehouse Moderator 68,457 PointsHey 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!!
Trenton Spears
Python Development Techdegree Graduate 16,969 PointsTrenton Spears
Python Development Techdegree Graduate 16,969 PointsI 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsI can try to walk you through it. Do you have a local environment set up to exercise code?
Trenton Spears
Python Development Techdegree Graduate 16,969 PointsTrenton Spears
Python Development Techdegree Graduate 16,969 PointsI'll open up VS Code right now
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsLetβs solve it without a function first.
Copy in the example dictionary (above) and assign it to a variable:
set
pets
equal to the βpetsβ item in the dictdata
:pets = data[βpetsβ]
pets
is now a list of dictionaries. Loop throughpets
extracting the βbreedβ information from each dict:Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsThe 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.
Trenton Spears
Python Development Techdegree Graduate 16,969 PointsTrenton Spears
Python Development Techdegree Graduate 16,969 PointsIf I set this up in my editor as instructed then should I still be getting a TypeError?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsSorry. My typo. Should be
dct[βbreedsβ]
using square brackets.Trenton Spears
Python Development Techdegree Graduate 16,969 PointsTrenton Spears
Python Development Techdegree Graduate 16,969 PointsGot it! Thanks Chris, I really appreciate your time and assistance man.