dictionary of list from a file

Peter Otten __peter__ at web.de
Thu Oct 5 08:13:18 CEST 2006


sjdevnull at yahoo.com wrote:

> d = {}
> for line in [l[:-1] for l in file('test.txt', 'rU') if len(l)>1]:
>     k,v = line.split()
>     d.setdefault(k,[]).append(v)

Try that with a test.txt where the last line has no newline.

Peter



More information about the Python-list mailing list