Looping to return value within dictionary in dictionary/list nest – must be integer or slices not str

First of all – I love that you presented this with a test for correctness. Awesome.

Secondly, you are accessing a list of dicts inside a list – which can be a tad confusing. Assuming that neither the test function nor the structure of cc_pet_shop can be changed, and also taking some liberties with how I present this based on your apparent use of unittest, this is a working solution:

import unittest

def get_pets_by_breed(pet_dict, breed):
    results = []
    for pet in pet_dict['pets']:
        if pet['breed'] == breed:
            results.append(pet)
    return results

class LegiScanTestCase(unittest.TestCase):
    
    def setUp(self):
        self.cc_pet_shop = {"pets": [
                                {
                                    "name": "Sir Percy",
                                    "pet_type": "cat",
                                    "breed": "British Shorthair",
                                    "price": 500
                                },
                                {
                                    "name": "King Bagdemagus",
                                    "pet_type": "cat",
                                    "breed": "British Shorthair",
                                    "price": 500
                                },
                                {
                                    "name": "Sir Lancelot",
                                    "pet_type": "dog",
                                    "breed": "Pomsky",
                                    "price": 1000,
                                },
                                {
                                    "name": "Arthur",
                                    "pet_type": "dog",
                                    "breed": "Husky",
                                    "price": 900,
                                },
                                {
                                    "name": "Tristan",
                                    "pet_type": "cat",
                                    "breed": "Basset Hound",
                                    "price": 800,
                                },
                                {
                                    "name": "Merlin",
                                    "pet_type": "cat",
                                    "breed": "Egyptian Mau",
                                    "price": 1500,
                                }
                            ]
            }
    
    def test_all_pets_by_breed__found(self):
        pets = get_pets_by_breed(self.cc_pet_shop, 'British Shorthair')
        self.assertEqual(2, len(pets))
        
if __name__ == '__main__':
    unittest.main()

Here’s an alternate implementation of your function that accomplishes the same thing in a single line using a list comprehension:

def get_pets_by_breed_alt(pet_dict, breed):
    return [pet for pet in pet_dict['pets'] if pet['breed'] == breed]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top