convert dictionary to string

Stephen D Evans sde at recombinant.demon.co.uk
Wed Apr 9 19:56:00 EDT 2003


d={'A':45.10, 'F':56.50,}

# assuming that you would like 2 decimal places...

s1 = ''.join(['%s%.2f' % item for item in d.iteritems()])

# or without the list comprehension...

s2 = ''
for (key, value) in d.items():
    s2 = '%s%s%.2f' % (s2, key, value)


Stephen D Evans


"matthew" <matthew at newsgroups.com> wrote in message
news:b72a7l$ifj$1 at lust.ihug.co.nz...
> hi,
>
> can anyone tell what is the fastest way to convert a dictionary to a
string?
>
> eg: d={'A':45.10, 'F':56.50,...}
> to 'A45.10F56.50...'
>
> Thanks very much. matthew
>






More information about the Python-list mailing list