Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to print dictionary in one line?

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!

like image 411
yusuke0426 Avatar asked Sep 19 '16 16:09

yusuke0426


1 Answers

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.

like image 103
PyNoob Avatar answered Sep 21 '22 16:09

PyNoob