trouble building data structure

Chris Angelico rosuav at gmail.com
Sun Sep 28 20:20:20 EDT 2014


On Mon, Sep 29, 2014 at 10:04 AM, David Alban <extasia at extasia.org> wrote:
>       file_data = {}
>
>       [... as i loop through lines in the file ...]
>
>           file_data[ md5sum ][ inode ] = { 'path' : path, 'size' : size, }
>
> what is the pythonic way to build my "file_data" data structure above that
> has the above structure?

The easiest way would be with a defaultdict. It's a subclass of
dictionary that does what you're looking for. You'd use it something
like this:

from collections import defaultdict
file_data = defaultdict(dict)
# then continue with the program as normal
# including the loop and assignments that you have above

Any time it's asked to look up an MD5 that doesn't exist yet, it'll
create a new dictionary by calling dict(), and that sets up the next
level for you.

Docs are here:
https://docs.python.org/2/library/collections.html#collections.defaultdict
https://docs.python.org/3/library/collections.html#collections.defaultdict

You can also use the setdefault() method of the regular dictionary,
which makes sense if you have just a few places where you need this
behaviour.

ChrisA



More information about the Python-list mailing list