Skip to content

Tag: dictionary

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…

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…

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…