key

Python delete item from a dictionary when double iterating

Python delete item from a dictionary when double iterating Question: I want to delet items from a dict where the value is a number and the key a datetime: Conditions: if the value is the same i want to delete all items with the same value within 1h #del duplicates within 60min for formatted_key in …

Total answers: 3

Python, list of nested repeated keys

Python, list of nested repeated keys Question: I would like to process this json data in python: {"data": [{"CU":"AGLD","CO": [{"chain":"ERC20"}], "fn":"Adventure Gold"}, {"CU":"ACH","CO": [{"chain":"ERC20"}, {"chain":"BEP20"}], "fn":"Alchemy"}]} I would like to get the value of all "chain"s. For example like this: CU chains: AGLD ERC20 ACH ERC20 ACH BEP20 I tried with a for loop, but …

Total answers: 2

Order of dictionary keys when performing conditional list comprehension in Python

Order of dictionary keys when performing conditional list comprehension in Python Question: In Python 3.10, I am aware that a dictionary preserves insertion order. However when performing conditional list comprehensions, can this order still be guaranteed? For example, given: my_dict = {} my_dict[‘a’] = 1 my_dict[‘b’] = 2 my_dict[‘c’] = 3 my_dict[‘d’] = 4 Can …

Total answers: 2

For a unique key ID get all other keys' values python dictionary

For a unique key ID get all other keys' values python dictionary Question: dictionary_name = [{‘id’:’123′,’id_2′:’5676′},{‘id’: ‘123’,’id_2′:’4545′},{‘id’:’123′,’id_2′:’8375′},{‘id’:’156′,’id_2′:’9374′}] I need to get: result = { ‘id’: {‘123’: [5676, 4545, 8375]}, {‘156’: [9374]} } So far, I have tried the following result = {} for d in dictionary_name: identifier = d[‘id’] if identifier not in dictionary_name: result[identifier] …

Total answers: 1

Grouping list of dictionary by keys (in a nested dictionary)

Grouping list of dictionary by keys (in a nested dictionary) Question: I have a list of dictionaries like below (sample): {‘platform’: ‘amazonwebservicesaws’, ‘region’: ‘useastnorthernvirginia’, ‘data’: {‘on_demand_price’: {‘usd’: ‘40.00’}}} {‘platform’: ‘amazonwebservicesaws’, ‘region’: ‘useastnorthernvirginia’, ‘data’: {‘on_demand_price’: {‘eur’: ‘33.33’}}} {‘platform’: ‘amazonwebservicesaws’, ‘region’: ‘useastnorthernvirginia’, ‘data’: {‘capacity_storage_price’: {‘usd’: ‘23.00’}}} {‘platform’: ‘amazonwebservicesaws’, ‘region’: ‘useastnorthernvirginia’, ‘data’: {‘capacity_storage_price’: {‘eur’: ‘19.17’}}} {‘platform’: ‘amazonwebservicesaws’, ‘region’: …

Total answers: 1

Replace key in a senetence with its value

Replace key in a senetence with its value Question: I have a dictionary and a list dict1= {‘good’:’bad’,’happy’:’sad’,’pro’:’anti’} list1=[‘she is good’,’this is a product’,’they are pro choice’] newlist=[] for index, data in enumerate(list1): for key, value in dict1.items(): if key in data: list1[index]=data.replace(key, dict1[key]) newlist.append(list1[index]) The output I get is [‘she is bad’,’this is a …

Total answers: 2

Get all the keys from json reponse – Python

Get all the keys from json reponse – Python Question: My json data would look like this { "a":1, "b":[ { "c":2, "d":{ "e":3 }, "f":{ "g":4 }, "h":[ { "i":5 }, { "j":6 } ] } ] } Is there a way I can extract all the keys from the response. What I would …

Total answers: 4

How to filter list of dictionaries by certain Keys?

How to filter list of dictionaries by certain Keys? Question: I have the following list: list = [{‘Jim’: {‘age’: 20, ‘lastname’: ‘Smith’}}, {‘Sarah’: {‘age’: 25, ‘lastname’: ‘Jones’}}, {‘Bill’: {‘age’: 30, ‘lastname’: ‘Lee’}}] I want to be able to filter list by Key, so for example if i want the the Sarah dict, i want the …

Total answers: 2