Home > OS >  drop value from a nested list of dictionaries
drop value from a nested list of dictionaries

Time:03-23

I have the following data:

[
    {
        "name": "Book1",
        "details": [
            {
                "id": 30278752,
                "isbn": "1594634025",
                "work_reviews_count": 3313007,
                "average_rating": "3.92"
            }
        ]
    },
    {
        "name": "Book2",
        "details": [
            {
                "id": 34006942,
                "isbn": "1501173219",
                "work_reviews_count": 2142280,
                "work_text_reviews_count": 75053,
                "average_rating": "4.33"
            }
        ]
    }
]

I want to drop work_text_reviews_count/"work_reviews_count" and isbn from the data. is that possible to do it with pandas? What's the most efficient way of doing this?

CodePudding user response:

You can Iterate over the list of dicts and delete the keys that you want:

dicts = [
    {
        "name": "Book1",
        "details": [
            {
                "id": 30278752,
                "isbn": "1594634025",
                "work_reviews_count": 3313007,
                "average_rating": "3.92"
            }
        ]
    },
    {
        "name": "Book2",
        "details": [
            {
                "id": 34006942,
                "isbn": "1501173219",
                "work_reviews_count": 2142280,
                "work_text_reviews_count": 75053,
                "average_rating": "4.33"
            }
        ]
    }
]

for d in dicts:
    if "details" in d:
        for d2 in d["details"]:
            if "work_text_reviews_count" in d2:
                del d2["work_text_reviews_count"]
            if "work_reviews_count" in d2:
                del d2["work_reviews_count"]
            if "isbn" in d2:
                del d2["isbn"]

print(dicts)

The output will be:

[{'name': 'Book1', 'details': [{'id': 30278752, 'average_rating': '3.92'}]}, {'name': 'Book2', 'details': [{'id': 34006942, 'average_rating': '4.33'}]}]

CodePudding user response:

To delete an entry in a dictionnary use "del".

Always check that the entry really exists in the dictionnary before deleting it.

mylist = [
        {
            "name": "Book1",
            "details": [
                {
                    "id": 30278752,
                    "isbn": "1594634025",
                    "work_reviews_count": 3313007,
                    "average_rating": "3.92"
                }
            ]
        },
        {
            "name": "Book2",
            "details": [
                {
                    "id": 34006942,
                    "isbn": "1501173219",
                    "work_reviews_count": 2142280,
                    "work_text_reviews_count": 75053,
                    "average_rating": "4.33"
                }
            ]
        }
       ]


for item in mylist:
    
    dic =  item["details"][0]
    
    if "id" in dic:
        del dic["id"]
    
    if "isbn" in dic:
        del dic["isbn"] 

    if "work_reviews_count" in dic:
        del dic["work_reviews_count"]
        
    if "work_text_reviews_count" in dic:
        del dic["work_text_reviews_count"]
        

print(mylist)

CodePudding user response:

You could delete (del) keys from the dictionaries or you could reconstruct them like this:

dicts = [
    {
        "name": "Book1",
        "details": [
            {
                "id": 30278752,
                "isbn": "1594634025",
                "work_reviews_count": 3313007,
                "average_rating": "3.92"
            }
        ]
    },
    {
        "name": "Book2",
        "details": [
            {
                "id": 34006942,
                "isbn": "1501173219",
                "work_reviews_count": 2142280,
                "work_text_reviews_count": 75053,
                "average_rating": "4.33"
            }
        ]
    }
]

ignore = ['work_text_reviews_count', 'work_reviews_count', 'isbn']
for d in dicts:
    if 'details' in d:
        for i, sd in enumerate(d['details']):
            d['details'][i] = {k: v for k, v in d['details'][i].items() if k not in ignore}
print(dicts)
  • Related