133

I've just started to learn python and I'm building a text game. I want an inventory system, but I can't seem to print out the dictionary without it looking ugly.

This is what I have so far:

def inventory():
    for numberofitems in len(inventory_content.keys()):
        inventory_things = list(inventory_content.keys())
        inventory_amounts = list(inventory_content.values())
        print(inventory_things[numberofitems])
3
  • 4
    "nicely" is a subjective term, but perhaps you are looking for something like this. Jun 22, 2017 at 3:19
  • 1
    FYI, this sample code doesn't work at all. The for loop here will be an error. I think you meant to use a range. Also, inventory_amounts isn't used, so you aren't printing the values.
    – sudo
    Jun 22, 2017 at 3:19

9 Answers 9

165

I like the pprint module (Pretty Print) included in Python. It can be used to either print the object, or format a nice string version of it.

import pprint

# Prints the nicely formatted dictionary
pprint.pprint(dictionary)

# Sets 'pretty_dict_str' to the formatted string value
pretty_dict_str = pprint.pformat(dictionary)

But it sounds like you are printing out an inventory, which users will likely want shown as something more like the following:

def print_inventory(dct):
    print("Items held:")
    for item, amount in dct.items():  # dct.iteritems() in Python 2
        print("{} ({})".format(item, amount))

inventory = {
    "shovels": 3,
    "sticks": 2,
    "dogs": 1,
}

print_inventory(inventory)

which prints:

Items held:
shovels (3)
sticks (2)
dogs (1)
5
  • 12
    or from pprint import pprint; pprint(dictionary) ... +1 Oct 16, 2018 at 3:08
  • 1
    Had to use: for item, amount in dct.items(): instead of: for item, amount in dct.iteritems():
    – Abisdad
    Nov 20, 2019 at 22:06
  • Hi, does this work if I want to save the output as a .txt file? Will it be saved in that format?
    – Jalkhov
    Jun 2, 2020 at 18:18
  • 1
    to keep the printing format of pprint, you can use import pprint as pp the n call pp.pformat(object) to keep its string structure for logging, saving, etc...
    – dtlam26
    Jun 7, 2022 at 9:03
  • You may need to use pprint.pprint(dictionary, sort_dicts=False) if dictionary order matters. Sep 27, 2022 at 15:50
98

My favorite way:

import json
print(json.dumps(dictionary, indent=4, sort_keys=True))
4
  • 2
    Note that sort_keys=True throws an error if there are keys that are of different types (eg. integer and string keys).
    – Kevin
    May 8, 2018 at 5:58
  • 5
    Your dictionary must only contain JSON-serializable objects, which are strings, various numbers, and booleans, for this to work. If that's the case, this is the easiest way with the nicest formatting.
    – sudo
    Jul 31, 2018 at 18:31
  • 14
    @sudo you can use default=str so if something is not JSON-serializable it is first converted to a string
    – arod
    Dec 5, 2018 at 20:30
  • Can it be sorted based on values?
    – alper
    Jan 1, 2021 at 15:01
27

Here's the one-liner I'd use. (Edit: works for things that aren't JSON-serializable too)

print("\n".join("{}\t{}".format(k, v) for k, v in dictionary.items()))

Explanation: This iterates through the keys and values of the dictionary, creating a formatted string like key + tab + value for each. And "\n".join(... puts newlines between all those strings, forming a new string.

Example:

>>> dictionary = {1: 2, 4: 5, "foo": "bar"}
>>> print("\n".join("{}\t{}".format(k, v) for k, v in dictionary.items()))
1   2
4   5
foo bar
>>>

Edit 2: Here's a sorted version.

"\n".join("{}\t{}".format(k, v) for k, v in sorted(dictionary.items(), key=lambda t: str(t[0])))
2
  • Can it be sorted based on the values?
    – alper
    Jan 1, 2021 at 15:00
  • 1
    @alper Yes, with something like "\n".join("{}\t{}".format(k, v) for k, v in sorted(dictionary.items(), key=lambda t: str(t[0])))
    – sudo
    Jan 13, 2021 at 4:21
7

I would suggest to use beeprint instead of pprint.

Examples:

pprint

{'entities': {'hashtags': [],
              'urls': [{'display_url': 'github.com/panyanyany/beeprint',
                        'indices': [107, 126],
                        'url': 'https://github.com/panyanyany/beeprint'}],
              'user_mentions': []}}

beeprint

{
  'entities': {
    'hashtags': [],
    'urls': [
      {
        'display_url': 'github.com/panyanyany/beeprint',
        'indices': [107, 126],
        'url': 'https://github.com/panyanyany/beeprint'}],
      },
    ],
    'user_mentions': [],
  },
}
1
  • but how do you use it? I tried from beeprint import pp just like in the doc, then tried calling pp(dictionary_name) but it doesn't work. Mar 12, 2022 at 9:24
3

Yaml is typically much more readable, especially if you have complicated nested objects, hierarchies, nested dictionaries etc:

First make sure you have pyyaml module:

pip install pyyaml

Then,

import yaml
print(yaml.dump(my_dict))
1
  • sudo apt install python-yaml for Ubuntu 16.04 users. There is a small flaw when a dictionary value string contains , but that is minor output flaw. Otherwise a great tool. Sep 27, 2020 at 22:49
0

I wrote this function to print simple dictionaries:

def dictToString(dict):
  return str(dict).replace(', ','\r\n').replace("u'","").replace("'","")[1:-1]
1
  • In some cases, such as {"abc, abc": 123, "def": 456}, will work incorrectly. Nov 21, 2019 at 12:05
0

Agree, "nicely" is very subjective. See if this helps, which I have been using to debug dict

for i in inventory_things.keys():
    logger.info('Key_Name:"{kn}", Key_Value:"{kv}"'.format(kn=i, kv=inventory_things[i]))
0

I did create function (in Python 3):

def print_dict(dict):
    print(

    str(dict)
    .replace(', ', '\n')
    .replace(': ', ':\t')
    .replace('{', '')
    .replace('}', '')

    )
0

Maybe it doesn't fit all the needs but I just tried this and it got a nice formatted output So just convert the dictionary to Dataframe and that's pretty much all

pd.DataFrame(your_dic.items())

You can also define columns to assist even more the readability

pd.DataFrame(your_dic.items(),columns={'Value','key'})

So just give a try :

print(pd.DataFrame(your_dic.items(),columns={'Value','key'}))

Not the answer you're looking for? Browse other questions tagged or ask your own question.