File to dict

Duncan Booth duncan.booth at invalid.invalid
Fri Dec 7 08:43:51 EST 2007


Matt Nordhoff <mnordhoff at mattnordhoff.com> wrote:

> Using two list comprehensions mean you construct two lists, which sucks
> if it's a large file.

Only if it is very large. You aren't duplicating the data except for 
entries with whitespace round them. If there isn't a lot of whitespace then 
the extra overhead for duplicating the list is unlikely to be significant.

> 
> Also, you could pass the list comprehension (or better yet a generator
> expression) directly to dict() without saving it to a variable:
> 
>     with open('/etc/virtual/domainowners','r') as fh:
>         return dict(line.strip().split(':', 1) for line in fh)
> 
> (Argh, that doesn't .strip() the key and value, which means it won't
> work, but it's so simple and elegant and I'm tired enough that I'm not
> going to add that. :-P Just use another genexp. Makes for a line
> complicated enough that it could be turned into a for loop, though.)

It isn't hard to convert my lists to generators keeping the structure 
exactly the same (and fixing the typo):

def loaddomainowners(domain):
    with open('/etc/virtual/domainowners','r') as infile:
        pairs = (line.split(':',1) for line in infile if ':' in line)
        pairs = ((domain.strip(), owner.strip())
            for (domain,owner) in pairs)
        return dict(pairs)




More information about the Python-list mailing list