Writing dictionary to file

effbot at pythonware.com effbot at pythonware.com
Fri Sep 8 12:58:34 EDT 2000


dan wrote:
> | f.write(join(map(lambda x:"%s %s"%x,dict.items()),'\n'))
>
> Or, in Python 2.0,
>
>   f.write(join([("%s %s" % x) for x in dict.items()],'\n'))

or better:

    f.writelines([("%s %s\n" % x) for x in dict.items()])

but I doubt list comprehensions are really the right thing
for things like this; I think I'll stick to:

    for item in dict.items():
        f.write("%s %s\n" % item)

</F>

<!-- daily news from the python universe:
http://www.pythonware.com/daily/index.htm
-->


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list