Skip to content

Tag: dictionary

Compare dictionaries ignoring specific keys

How can I test if two dictionaries are equal while taking some keys out of consideration. For example, should return True. UPD: I’m looking for an efficient, fast solution. UPD2. I ended up with this code, which appears to be the fastest: Timings: https://gist.github.com/2651872 Answer EDIT: This might …

Remove duplicate dict in list in Python

I have a list of dicts, and I’d like to remove the dicts with identical key and value pairs. For this list: [{‘a’: 123}, {‘b’: 123}, {‘a’: 123}] I’d like to return this: [{‘a’: 123}, {‘b’: 123}] Another example: For this list: [{‘a&…

How to check if a value exists in a dictionary?

I have the following dictionary in python: I need a way to find if a value such as “one” or “two” exists in this dictionary. For example, if I wanted to know if the index “1” existed I would simply have to type: And then python would tell me if that is true or false, howeve…

Converting xml to dictionary using ElementTree

I’m looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes. Answer Call as This works as long as you don’t actually have an attribute text; if you do, then change the third line in the functio…

Is it reasonable to use None as a dictionary key in Python?

None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works: The actual data I am working with is educational standards. Every standard is associated with a content area. Some standards are also associated with content subareas. I would like to m…