Dictionary self lookup

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jun 24 18:26:30 CEST 2009


Norberto Lopes wrote:
> On Jun 24, 1:21 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Norberto Lopes wrote: ...
>>> config = {"home" : "/home/test"}
>>> config["user1"] = config["home"] + "/user1"
>>> config["user2"] = config["home"] + "/user2"
>>> config["python-dev"] = config["user1"] + "/py-dev"

I'd write this as:
    home = "/home/test"
    config = {"home" : home, "user1" : home + "/user1",
              "user2" : home + "/user2", "python-dev" : home + "/py-dev"}

or even (if the list gets much longer):

    home = "/home/test"
    config = {"home" : "", "user1" : "/user1", "user2" : "/user2",
              "python-dev" : "/py-dev"}
    for key, entry in config.iteritems():
        config[key] = home + entry

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list