Skip to content

Tag: dictionary

JSON get key path in nested dictionary

Here is my json string, which keeps on changing frquently so the keys position within the dictionary is not same everytime, i need to search for a key and print it corresponding value, Since the json string changes everytime I have written an recursive function(See below) to search for key in the new json str…

How to add multiple values per key in python dictionary

My program needs to output a list of names with three numbers corresponding to each name however I don’t know how to code this is there a way I could do it as a dictionary such as cat1 = {“james”:6, “bob”:3} but with three values for each key? Answer The value for each key can ei…

Print a dictionary into a table

I have a dictionary: I would like to output it as: Is it a good way to first convert them into a list of dictionaries, and then write them into a table, by the method in https://stackoverflow.com/a/10373268/156458? Or is there a way better in some sense? Answer Rather than convert to a list of dictionaries, d…

Slicing a dictionary

I have a dictionary, and would like to pass a part of it to a function, that part being given by a list (or tuple) of keys. Like so: Now, ideally I’d like to be able to do this: … but that’s not working, since it will look for a key matching the tuple (1,5), the same as d[1,5]. d{1,5}

Disabling sorting mechanism in pprint output

I have big dictionary which I`m printing for viewing with prettyprint, but how I can keep formatting but kill sorting mechanism in pprint? Answer Python 3.8 or newer: Use sort_dicts=False: Python 3.7 or older: You can monkey patch the pprint module. Since the 2nd output is essentiallly randomly sorted, your o…