Skip to content
Advertisement

How do i delete an element of a json object with python?

I’m trying to delete elements from _notes that have _type as 1, but i keep getting an error and I’m not sure what it means, nor do I know how to fix it.. can anyone help me?

My trimmed JSON:

JavaScript

My python script:

JavaScript

Error: Traceback (most recent call last): File "C:programmingpythontrain_one_handrun.py", line 9, in <module> obj.pop(obj["_notes"][i]) TypeError: unhashable type: 'dict'

Advertisement

Answer

It is usually a bad idea to delete from a list that you’re iterating. Reverse iterating avoids some of the pitfalls, but it is much more difficult to follow code that does that, so usually you’re better off using a list comprehension or filter.

JavaScript

This gives us the expected output :

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement