I’m looking for the most efficient and pythonic (mainly efficient) way to update a dictionary but keep the old values if an existing key is present. For example… notice how the key ‘2’ exists in both dictionaries and used to have values (‘3’, ‘1’) but now it has…
Tag: dictionary
Output pyodbc cursor results as python dictionary
How do I serialize pyodbc cursor output (from .fetchone, .fetchmany or .fetchall) as a Python dictionary? I’m using bottlepy and need to return dict so it can return it as JSON. Answer If you don’t know columns ahead of time, use Cursor.description to build a list of column names and zip with each…
How to print a dictionary line by line in Python?
This is the dictionary Using this for loop It prints the following: But I want the program to print it like this: I just started learning dictionaries so I’m not sure how to do this. Answer output:
Grouping Python dictionary keys as a list and create a new dictionary with this list as a value
I have a python dictionary Since the values in the above dictionary are not unique, I want to group the all the keys of unique values as a list and create a new dictionary as follows: Note the keys of new dictionary v should be sorted. I am finding it hard to visualize and implement this dictionary creation. …
“Flattening” a list of dictionaries
So my aim is to go from: to A way I got is: Is there any better more pythonic way? Answer
Finding a key recursively in a dictionary
I’m trying to write a very simple function to recursively search through a possibly nested (in the most extreme cases ten levels deep) Python dictionary and return the first value it finds from the given key. I cannot understand why my code doesn’t work for nested dictionaries. It returns None. It…
How to get the index with the key in a dictionary?
I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary, Is there a combination of python functions so that I can get the index value of 1, given the key value ‘b’? I know it can be achieved with a loop or lambda (w…
Only add to a dict if a condition is met
I am using urllib.urlencode to build web POST parameters, however there are a few values I only want to be added if a value other than None exists for them. That works fine, however if I make the orange variable optional, how can I prevent it from being added to the parameters? Something like this (pseudocode…
Find a value within nested json dictionary in python
From the following json, in python, I’d like to extract the value “TEXT”. All the keys are constant except for unknown. Unknown could be any string like “a6784t66” or “hobvp*nfe”. The value of unknown is not known, only that it will be in that position in each json re…
Recursive DotDict
I have a utility class that makes Python dictionaries behave somewhat like JavaScript objects as far as getting and setting attributes. I would like to make it so it also converts nested dictionaries into DotDict() instances. I was hoping to be able to do something like this with __init__ or __new__, but I ha…