dictionary of list from a file

Peter Otten __peter__ at web.de
Thu Oct 5 12:11:06 CEST 2006


sjdevnull at yahoo.com wrote:

> for line in (l.rstrip("\n") for l in file("test.txt", "rU") if l[0] !=
> "\n"):
>    k, v = line.split()
>    d.setdefault(k, []).append(v)

Note that this snippet will produce the same output with or without the
rstrip() method call.

Peter



More information about the Python-list mailing list