2

I have a dictionary in Python that looks as follows:

{'first':{0,1,2}, 'second':{2,3}, etc .. }

There are lots of items so I can not scroll over them in the cmd. Therefore, I tried some methods to write the output to a file but it did not work with me (see: Error in writing a dictionary to a file).

Now, I need a method that allows me to view the dictionary content and also search over the content (ctrl-F or similar). I am a new to python so kindly bear this in mind and try to answer me in step-by-step fashion

Community
  • 1
  • 1
user2192774
  • 3,807
  • 17
  • 47
  • 62

1 Answers1

3

Use pprint.pprint() to print formatted representations that are usually not that wide. You'll have to use your terminal functionality to search though. Use Python itself to search through dictionaries:

print next((key, dictionary[key] for key in dictionary if 'some_search' in key), None)

would find a key-value pair where some_search is contained in the key value.

You may want to look into an alternative shell such as iPython, or an alternative terminal for Windows that offers a little more functionality over the bare-bones CMD.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343