key-value

Get value from nested dictionary in Python

What is the proper way to extract the value from nested dictionary in Python? Question: A nested dictionary: nested_dict = {"fruit": {"apple":{"status": "new", "sold": True}, "banana": 10, "watermelon": 30}, "meat": {"red": 39, "white": 13}} res = nested_dict.get("fruit", {}).get("apple", {}).get("status") if res: print(f"{res = }") Is there any better practise to extract the value from the …

Total answers: 2

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

Re-defining a dict key value

Re-defining a dict key value Question: This code doesn’t work – is there another way I can add a key-value pair with a variable as the value? BTC_ = {"Name": "BTC", "liq": 57000} ETH_ = {"Name": "ETH", "liq": 50000} BTC = BTC_[‘liq’] ETH = ETH_[‘liq’] ETHp = ETH / 50000 BTCp = BTC / 50000 …

Total answers: 2

How to select a certain key value and skip if a certain key is not present in the list of dictionaries?

How to select a certain key value and skip if a certain key is not present in the list of dictionaries? Question: d = [{’email’: ‘[email protected]’, ‘gid’: ‘5b869a4fe1cd8e14a38d67b5’, ‘_id’: ’53f49508dabfaeb4c677b4a4′, ‘name’: ‘Hiromasa Habuchi’, ‘org’: ‘Department of Mathematics and Statistics, University of Victoria, Victoria, BC, V8W 3R4, Canada’, ‘orgid’: ‘5f71b2841c455f439fe3c6c9’}, {‘_id’: ’53f43522dabfaee4dc7780b2′}, {‘_id’: ‘560175f745cedb3395e5a530’}] d is …

Total answers: 1

How to convert list of lists to dict key value pairs python

How to convert list of lists to dict key value pairs python Question: I have a list of lists like so: splitted = [[‘OID:XXXXXXXXXXX1’, ‘ street:THE ROAD’, ‘town:NEVERPOOL’, ‘postcode:M1 2DD’, ‘Name:SOMEHWERE’, ‘street:THE ROAD’, ‘town:NEVERLAND’, ‘postcode:M1 2DD’], [‘OID:XXXXXXXXXXX2’, ‘ Name:30’, ‘street:DA PLACE’, ‘town:PERTH’, ‘postcode:PH1 2DD’, ‘Name:30’, ‘street:DA PLACE’, ‘town:PERTH’, ‘postcode:PH1 2DD’]] I’d like to convert these …

Total answers: 3

Key: value store in Python for possibly 100 GB of data, without client/server

Key: value store in Python for possibly 100 GB of data, without client/server Question: There are many solutions to serialize a small dictionary: json.loads/json.dumps, pickle, shelve, ujson, or even by using sqlite. But when dealing with possibly 100 GB of data, it’s not possible anymore to use such modules that would possibly rewrite the whole …

Total answers: 7

Iterate through dictionary values?

Iterate through dictionary values? Question: Hey everyone I’m trying to write a program in Python that acts as a quiz game. I made a dictionary at the beginning of the program that contains the values the user will be quizzed on. Its set up like so: PIX0 = {“QVGA”:”320×240″, “VGA”:”640×480″, “SVGA”:”800×600″} So I defined a …

Total answers: 5

get Key by value, dict, python

get Key by value, dict, python Question: How can I get a key from a value? my dict: countries = { “Normal UK project” : “1”, “UK Omnibus project” : “1-Omni”, “Nordic project” : [“11″,”12″,”13″,”14”], “German project” : “21”, “French project” : “31” } my semi functioning code: for k, v in countries.items(): if “1” …

Total answers: 7