10 examples of 'find key in dictionary python' in Python

Every line of 'find key in dictionary python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5def _find_dict_in_list(list_of_dict, key, val):
6 """Given a list of dictionaries, return the first dictionary that has the
7 given key:value pair.
8 """
9 try:
10 for d in list_of_dict:
11 if (key, val) in d.items():
12 return d
13 except TypeError:
14 pass
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

102def _find_key(d, key):
103 if not d:
104 return None
105 for k, v in d.items()[:]:
106 if k == key:
107 return d
108 else:
109 result = _find_key(v, key)
110 if result is not None:
111 return result
53def __contains__(self, key):
54 return str(key) in self.data
31def dict_lookup(dic, key_path, default=None):
32 """
33 Look up value in a nested dict
34
35 Args:
36 dic: The dictionary to search
37 key_path: An iterable containing an ordered list of dict keys to
38 traverse
39 default: Value to return in case nothing is found
40
41 Returns:
42 Value of the dict at the nested location given, or default if no value
43 was found
44 """
45
46 for k in key_path:
47 if k in dic:
48 dic = dic[k]
49 else:
50 return default
51 return dic
37def findkey(haystack, needle, default=None):
38 for k, v in haystack.items():
39 if v == needle:
40 return k
41 return default
565def TryGetValue(self,key,value):
566 """ TryGetValue(self: IDictionary[TKey,TValue],key: TKey) -> (bool,TValue) """
567 pass
84def __contains__(self, key):
85 if '.' not in key:
86 return dict.__contains__(self, key)
87 myKey, restOfKey = key.split('.', 1)
88 if myKey in self:
89 target = dict.__getitem__(self, myKey)
90 else:
91 return False
92 if not isinstance(target, dotdict):
93 return False
94 return restOfKey in target
315def __getitem__(self, key):
316 for k, v in self:
317 if key == k:
318 return v
319 raise KeyError
48def __getitem__(self, key):
49 if key in self.Dict[self.store]:
50 return self.Dict[self.store][key]
79def lookup(d, keys):
80 if not isinstance(keys, list):
81 keys = keys.split(".")
82 for k in keys:
83 if hasattr(d, "__getitem__"):
84 if k in d:
85 d = d[k]
86 else:
87 return None
88 else:
89 return None
90 return d

Related snippets