Skip to content
Advertisement

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 be faster and more memory-efficient:

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’: 123, ‘b’: 1234}, {‘a’: 3222, ‘b’: 1234}, {‘a’: 123, ‘b’: 1234}] I’d like to return this: [{‘a’:

custom dict that allows delete during iteration

UPDATED based on Lennart Regebro’s answer Suppose you iterate through a dictionary, and sometimes need to delete an element. The following is very efficient: The only overhead here is building the list of keys to remove; unless it grows large compared to the dictionary size, it’s not an issue. However, this approach requires some extra coding, so it’s not very

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, however I need to do

Walking/iterating over a nested dictionary of arbitrary depth (the dictionary represents a directory tree)

Python newbie at time of writing. This came up because I want a user to be able to select a group of files from within a directory (and also any subdirectory), and unfortunately Tkinter’s default ability for selecting multiple files in a file dialog is broken on Windows 7 (http://bugs.python.org/issue8010). So I am attempting to represent a directory structure by

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 function body to use a

How to merge dictionaries of dictionaries?

I need to merge multiple dictionaries, here’s what I have for instance: With A B C and D being leaves of the tree, like {“info1″:”value”, “info2″:”value2”} There is an unknown level(depth) of dictionaries, it could be {2:{“c”:{“z”:{“y”:{C}}}}} In my case it represents a directory/files structure with nodes being docs and leaves being files. I want to merge them to obtain:

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 make a nested dictionary of the

Advertisement