Hi I am new to Python and I am struggling regarding how to print dictionary.
I have a dictionary as shown below.
dictionary = {a:1,b:1,c:2}
How can I print dictionary in one line as shown below?
a1b1c2
I want to print keys and values in one line but could not figure out by myself.
I would appreciate your advice!
With a dictionary, e.g.
dictionary = {'a':1,'b':1,'c':2}
You could try:
print ''.join(['{0}{1}'.format(k, v) for k,v in dictionary.iteritems()])
Resulting in
a1c2b1
If order matters, try using an OrderedDict, as described by this post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With