dictionary

python dict get method runs the second argument even when key is in dict

python dict get method runs the second argument even when key is in dict Question: according to the doc, the get method of a dict can have two argument: key and a value to return if that key is not in the dict. However, when the second argument is a function, it’ll run regardless of …

Total answers: 2

Printing nested dictionary

Printing nested dictionary Question: I have 2 dictionaries: movie_collections_dict = {‘Avatar’:0, ‘Titanic’:0, ‘StarWar’:0} #which indicates the revenues of the movies and {‘avatar’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}, ‘starwar’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}, ‘titanic’: {‘adult’: 0, ‘child’: 0, ‘concession’: 0, ‘senior’: 0, ‘student’: 0}} #which …

Total answers: 1

Obtaining information from a list/dict in Python

Obtaining information from a list/dict in Python Question: I understand this question may have been asked in many different formats in the past. I also checked the recommended answers when I wrote the question title, and I haven’t found what I need. I’m new to Python so my understanding of lists and dictionaries is slowly …

Total answers: 2

Function to dynamically extract values from multiple nested dictionary in Python

Function to dynamically extract values from multiple nested dictionary in Python Question: The problem statement is to write a function which will take a input dictionary object and return a list of all the values, even in case of multi-level nested dictionaries inside the input dictionary object. Example of such a nested dictionary below: { …

Total answers: 3

generate a new dictionary based on list of values

generate a new dictionary based on list of values Question: I have an input dictionary d1 and list l1 and want to generate output dictionary d2. d1 = {‘A1’:[‘b1′,’b2′,’b3’], ‘A2’:[‘b2’, ‘b3’], ‘A3’:[‘b1’, ‘b5’]} l1 = [‘b2’, ‘b5’, ‘b1’, ‘b3’] Output dictionary d2 = {‘b2’:[‘A1′,’A2’], ‘b5’:[‘A3’], ‘b1’:[‘A1′,’A3’], ‘b3’:[‘A1′,’A2’]} In output dictionary all values of list l1 …

Total answers: 1

How to flatten list of dictionaries

How to flatten list of dictionaries Question: Learning how to traverse lists and dictionaries in Python. process = [ { ‘process1’: [ {"subprocess1":["subprocess1_1","subprocess1_2"]}, "subprocess2", {"subprocess3":["subprocess3_1", "subprocess3_2"]}, "subprocess4", {"subprocess5":[{"subprocess5_1":["subprocess5_1_1","subprocess5_1_2"]}]}, ], }, { ‘process2’: [ "subprocess2_1" ] } ] How do I flatten above list of dictionaries into following: process1 = [subprocess1, subprocess1_1, subprocess1_2, subprocess2, subprocess3, subprocess3_1, …

Total answers: 3

Python nested dictionary – remove "" and data with extra spaces but keep None values

Python nested dictionary – remove "" and data with extra spaces but keep None values Question: I have a dictionary and would like to keep None values but remove values with "" and also values of any combination of " "’s I have the following dictionary: {‘UserName’: ”, ‘Location’: [{‘City’: ”, ‘Country’: ‘Japan’, ‘Address 1’: …

Total answers: 2

How to group the same values into same key?

How to group the same values into same key? Question: I have an assignment to do this things without import any helpful function, could you guys help me? Thanks! data = [ [‘num’,’e1′,’e2′,’e3′], [‘2002′,’p’,’r’,’i’], [‘2002′,’k’,’r’,’i’], [‘2001′,’k’,’r’,’e’], [‘2004′,’p’,’a’,’p’], [‘2004′,’p’,’s’,’f’] ] newlist = [ {‘num’: ‘2001’, ‘e1’: ‘k’, ‘e2’: ‘r’, ‘e3’: ‘e’}, {‘num’: ‘2002’, ‘e1’: ‘p’, ‘e2’: …

Total answers: 1

Why will python function max() return different outputs if float('NaN') value is permuted in a dictionary but key-max_value remains the same?

Why will python function max() return different outputs if float('NaN') value is permuted in a dictionary but key-max_value remains the same? Question: Let’s pretend I have the following simple dictionary: dictionary = {‘a’:3, ‘b’:4, ‘c’:float(‘NaN’)} If I use function max() to return the key with maximum value… key_maxvalue = max(dictionary, key=dictionary.get) print(key_maxvalue) …python outputs this: …

Total answers: 2