key-value

How to solve the value error received while working with this dictionary?

How to solve the value error received while working with this dictionary? Question: I have a dataframe from which the columns are grouped as: {(a, b, c): [(‘d’, e, f)]} with this command: dct = df.groupby([‘a’,’b’,’c’])[[‘d’,’e’,’f’]].apply( lambda g: list(map(tuple, g.values.tolist()))).to_dict() After this, I apply: dct = {k: dict(v) for k,v in dct.items()} which gives me …

Total answers: 1

Merge two dictionaries in python

Merge two dictionaries in python Question: I’m trying to merge two dictionaries based on key value. However, I’m not able to achieve it. Below is the way I tried solving. dict1 = {4: [741, 114, 306, 70], 2: [77, 325, 505, 144], 3: [937, 339, 612, 100], 1: [52, 811, 1593, 350]} dict2 = {1: …

Total answers: 6

How to delete dictionary values simply at a specific key 'path'?

How to delete dictionary values simply at a specific key 'path'? Question: I want to implement a function that: Given a dictionary and an iterable of keys, deletes the value accessed by iterating over those keys. Originally I had tried def delete_dictionary_value(dict, keys): inner_value = dict for key in keys: inner_value = inner_value[key] del inner_value …

Total answers: 2

How do I get a dict key from an int when the dict values are lists?

How do I get a dict key from an int when the dict values are lists? Question: I have the python dict: my_dict = {‘C’: [1,2,3,4,5,6,7,8,9,10,11,12,15,18,46,64,67,73,78,83], ‘B’: [13,14,22,32,38,39,42,59,68,74,79,84], ‘A’: [16,17,19,23,31,37,40,50,51,60,70,75,80,85], ‘S’: [20,25,33,36,43,44,48,49,57,61,62,63,71,76,81,86,88,89,92,94], ‘SS’: [21,24,26,47,52,53,54,56,66,69,72,77,82,87,91,93], ‘SS+’: [27,28,29,30,34,35,41,45,55,58,65,90,95,96,97,98], ‘SSS’: [99,100]} How would I get the key ‘C’ as the output when I give it the integer value …

Total answers: 1

How to split values of a Python dictionary

How to split values of a Python dictionary Question: I have a dictionary like this: data = { key1: [ <A><B> ] key2: [ <C><D> ] key3: [ <A><F> ] … } Goal to achieve: I want to have instead something like this: new_dict = { key1: [ <A>, <B> ] key2: [ <C>, <D> …

Total answers: 1

Loop Through Two Dataframe Columns to Create Key-Value Pairs For Value Dictionaries Nested Inside A Dictionary Nested Inside Another Dictonary

Loop Through Two Dataframe Columns to Create Key-Value Pairs For Value Dictionaries Nested Inside A Dictionary Nested Inside Another Dictonary Question: I want to create a metadata variable based on my input dataset’s columns using dictionaries. Specifically, I want the metadata variable to have exactly this structure and values: metadata = { ‘fields’: { ‘id’: …

Total answers: 1

python – extract value from list of dictionary

python – extract value from list of dictionary Question: I need to display book name and calculate average weight of book if the book price is more than 800. here is my current code, appreciate any help. thank you so much in advance! def calcWeight(): for book in books: for key,value in book.items(): if key …

Total answers: 3

Python using dictionary list values to key match a dictionary

Python using dictionary list values to key match a dictionary Question: I’m struggling in python on how to query a dictionary with a value from one list as a key in another. For example: d1 = {"fruit":[5, 3, 3, 3, 4, 2, 4]} d2 = {0:"apple", 1:"pear", 2:"cherry", 3:"lime", 4:"orange", 5:"mango", 6:"kiwi"} What I’d like …

Total answers: 2

compare values from dictionary with list

compare values from dictionary with list Question: i have a dictionary like this and i want to compare it with a list dic= {0: 0, 1: 1200, 2: 2400, 3: 1800, 4: 2400, 5: 2400, 6: 1200, 7: 1800, 8: 2400} list = [0,2,5,7] How can I create sum over the list with the values …

Total answers: 3