Access information of containing list in python
Question: I'm writing a script that can help me documenting our network rooms.

The idea behind that script is that a room is a list, that contains several
lists for the racks. The rack lists contain lists called module with the
servers/switches/etc. in the module list are the actual ports with the cable
numbers.

For example:

    
    
    [04/02, [MM02, [1, #1992, 2, #1993, 3, #1567 ....], MM03, [1, #1234 .....]], 04/03, [MM01, .........]]]
    

`04/02` = First Rack

`MM02` = First module in that rack

`1` = Port number

`#1992` = Cable number

I hope you get the idea.

The script I wrote compares the cable numbers in the room list, and looks if
there are duplicates. Now it gets tricky: It should then replace the cable
number with the rack and module of the other port. That should be pretty easy,
because module and rack are the first elements of those lists that contain the
port, but I don't know how to get access to the information. (I'm a noob in
programming)


Answer: As mentioned in the commends, the much better data structure to use here is
nested [`dict`s](http://docs.python.org/library/stdtypes.html#mapping-types-
dict):

    
    
    data = {
        "04/02": {
            "MM02": {1: "#1992", 2: "#1993", 3: "#1567", ...},
            "MM03": {1: "#1234", ...},
            ... 
        },
        "04/03": {
            "MM01": ...
            ...
        },
        ...
    }
    

Then you simply do `data["04/02"]["MM02"] = {1: "#1992", 2: "#1993", 3:
"#1567", ...}` to replace values, however, this has the disadvantage of
meaning you need to manually create the sub-dictionaries - there are, however,
[solutions to this problem](http://stackoverflow.com/questions/10218486/set-
nested-dict-value-and-create-intermediate-
keys/10218652#comment13126770_10218652) e.g:

    
    
    from functools import partial
    from collections import defaultdict
    
    tripledict = partial(defaultdict, partial(defaultdict, dict))
    mydict = tripledict()
    mydict['foo']['bar']['foobar'] = 25
    

These not only have advantages in readability and usability, but also speed of
access.

