[Solved] Dictionary all for one


First of all, just printing the dictionary itself will print everything on one line:

>>> dicMyDictionary = {"michael":"jordan", "kobe":"bryant", "lebron":"james"}
>>> print(dicMyDictionary)
{'kobe': 'bryant', 'michael': 'jordan', 'lebron': 'james'}

If you want to format it in some way:

>>> print(' '.join(str(key)+":"+str(value) for key,value in dicMyDictionary.iteritems()))
kobe:bryant michael:jordan lebron:james

ref: str.join, dict.iteritems

Or with a for loop for more detailed control:

>>> formatted_str = ""
>>> for key,value in dicMyDictionary.iteritems():
        formatted_str += "(key: " + str(key) + " value:" + value + ") "
>>> print(formatted_str)
(key: kobe value:bryant) (key: michael value:jordan) (key: lebron value:james)

ref: dict.iteritems,Python looping techniques

4

solved Dictionary all for one