well in python dir(dict) shows us available methods for dictionary, and "somemethod" in dir(dict) shows us if somemethod is available in sed data type. SO if we factor it in our problem we can se that in :
py.2.7->
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__',\
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',\
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__',\
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems',\
'iterkeys', 'itervalues','keys', 'pop', 'popitem', 'setdefault', 'update', 'values', \
'viewitems', 'viewkeys', 'viewvalues']
py.3.5 ->
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__',\
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',\
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__',\
'__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems',\
'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values',\
'viewitems', 'viewkeys', 'viewvalues']
Now we can see several things here, pop
,popitem
,has_key
,'get', and since dictionary in python is not ordered or sorted ( it prints it out as it goes ) we can also use update
.
So first we need to transverse trough list of dictionaries and condition trough items, depending on size of your dictionary map or dictionary comprehension could do the trick or just dict.items() which returns list of tuples.
so
def duDict(dictionary,codnitionElement,keyInQuesiton):
el = dictionary.pop(keyInQuesiton) #similar as list.pop
if condition(dictionary.get(codnitionElement)):
el = something
dictionary.update({keyInQuesiton:el })
else:pass
return dictionary
then something along the lines
condDict = lambda dict: duDict(dict,predefinedCondition,forKey)
map( condDict , listOfDicts )
Hope this helps