Every line of 'python get value from dictionary if key exists' 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.
565 def TryGetValue(self,key,value): 566 """ TryGetValue(self: IDictionary[TKey,TValue],key: TKey) -> (bool,TValue) """ 567 pass
433 class IReadOnlyList(IReadOnlyCollection[T], IEnumerable[T], IEnumerable): 434 # no doc 435 def __contains__(self, *args): #cannot find CLR method
316 def __getitem__(self, key): 317 """Return the first data value for this key; 318 raises KeyError if not found. 319 320 :param key: The key to be looked up. 321 :raise KeyError: if the key does not exist. 322 """ 323 if key in self: 324 lst = dict.__getitem__(self, key) 325 if len(lst) > 0: 326 return lst[0] 327 if self.key_error_exception: 328 raise self.key_error_exception(key)
5 def _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
102 def _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
31 def 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
92 def getDic(self, key = 'process'): 93 """return the list of processes of all active terminal""" 94 return [self.dic[terminal][key] for terminal in self.dic if self.dic[terminal]['active']]
1 def contains_val(data, key, value): 2 """ 3 check if certain key has certain value, return true if so and false otherwise 4 """ 5 # try to get value variable from parser specific variables 6 value = _ttp_["parser_object"].vars.get(value, value) 7 if not value in data.get(key, ""): 8 return data, False 9 return data, None
22 def _get_value_by_path(source_dict, key_path, 23 ignore_key_error=False, default=""): 24 """Get key value from nested dict by path. 25 26 Args: 27 source_dict: The dict that we look into. 28 key_path: A list has the path of key. e.g. [parent_dict, child_dict, key_name]. 29 ignore_key_error: Ignore KeyError if the key is not found in provided path. 30 default: Set default value if the key is not found in provided path. 31 32 Returns: 33 If key is found in provided path, it will be returned. 34 If ignore_key_error is True, None will be returned. 35 If default is defined and key is not found, default will be returned. 36 """ 37 38 key_value = "" 39 try: 40 # Reduce key path, where it get value from nested dict. 41 # a replacement for buildin reduce function. 42 for key in key_path: 43 if isinstance(source_dict.get(key), dict) and len(key_path) > 1: 44 source_dict = source_dict.get(key) 45 key_path = key_path[1:] 46 _get_value_by_path(source_dict, key_path, 47 ignore_key_error=ignore_key_error, default=default) 48 else: 49 key_value = source_dict[key] 50 51 # How to set the key value, if the key was not found. 52 except KeyError as key_name: 53 if default: 54 key_value = default 55 elif not default and ignore_key_error: 56 key_value = None 57 elif not key_value and not ignore_key_error: 58 sys.exit("The key %s is not found. Please remember, Python is case sensitive." % key_name) 59 return key_value
67 def __getitem__(self, key): 68 self.__missing__(key) 69 return self.value