I want to make a function that returns a copy of a dictionary excluding keys specified in a list. Considering this dictionary: A call to without_keys(my_dict, [‘keyB’, ‘keyC’]) should return: I would like to do this in a one-line with a neat dictionary comprehension but I’m havin…
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}
How can you slice with string keys instead of integers on a python OrderedDict?
Since an OrderedDict has the features of both a list (with ordered elements), and a dictionary (with keys instead of indexes), it would seem natural that you could slice using keys. What’s interesting about this is that it’s not the error you’d see due to OrderedDictionary.__getslice__ not b…
How does one ignore unexpected keyword arguments passed to a function?
Suppose I have some function, f: Now, if I have a dictionary such as dct = {“a”:”Foo”}, I may call f(**dct) and get the result Foo printed. However, suppose I have a dictionary dct2 = {“a”:”Foo”, “b”:”Bar”}. If I call f(**dct2) I get a Fa…
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…
Right way to initialize an OrderedDict using its constructor such that it retains order of initial data?
What’s the correct way to initialize an ordered dictionary (OD) so that it retains the order of initial data? Question: Will an OrderedDict preserve the order of a list of tuples, or tuple of tuples or tuple of lists or list of lists etc. passed at the time of initialization (2nd & 3rd example above…
Create Python DataFrame from dictionary where keys are the column names and values form the row
I am familiar with python but new to panda DataFrames. I have a dictionary like this: And I would like to convert it to a DataFrame, where b and c are the column names, and the first row is 100,300 (100 is underneath b and 300 is underneath c). I would like a solution that can be generalized to a