Skip to content

Tag: dictionary

Getting Key in Nested Dictionary

I have a dictionary like this: Now I want to extract the key whenever a value from that key is searched. For example if user inputs mango it should print fruit How can I do this? Answer There is no way to directly do this to my knowledge, but you can try an iterative approach over the dictionary’s value…

Path inside the dictionary from the variable

I have a code: Now I have an error: But if I try: Everything is okay. How I should send the path to the list from the JSON’s dictionary — from one function to other? Answer You cannot pass language syntax elements as if they were data strings. Similarly, you could not pass the string “2 > 1 and…

how to add file names into dictionary based on their prefix?

I have a following problem. I have a list containing file names: I need to add them into dictionary based on their prefix number, i.e. 12 and 23. Each value of the dictionary should be a list containing all files with the same prefix. Desired output is: What I tried so far: However this gives me the result {&…

Using eval within a dict

I want to evaluate the value of a dict key, using the dict itself. For example: When I do this it includes a bunch of unnecessary stuff in the dict. In the above example it prints: Instead, in the above example I just want: How to resolve this issue? Thank you! Answer Pass a copy of dict to eval(): Prints:

Sort a list by value of a dict python

I need some help, how to use the algorithm selection sort to sort a list by the values of a dict. I wrote some code but I don’t know how continue, that the code work. e.g. sort the list by the values of the dict Answer New Answer Create class selection_sort which has get_sorted() method that will sort t…