hjelpmig
hjelpmig

Reputation: 1455

Saving dictionary keys as strings

I have following dictionary:

OrderedDict([(u'b1', OrderedDict([(u'ip', u'199.0.0.1'), (u'port', u'1122')])),
             (u'b2', OrderedDict([(u'ip', u'199.0.0.1'), (u'port', u'1123')]))])

I want to create a string that take keys from the dictionary and join them but also put : in between, so the result will be

(b1:b2)

there can be a lot of keys in the dictionary. Can somebody help me out with that?

Upvotes: 1

Views: 107

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Use str.join

>>> from collections import OrderedDict
>>> dic =  OrderedDict([(u'b1', OrderedDict([(u'ip', u'199.0.0.1'), (u'port', u'1122')])), (u'b2', OrderedDict([(u'ip', u'199.0.0.1'), (u'port', u'1123')]))])
>>> ":".join(dic)
u'b1:b2'

Upvotes: 5

Related Questions