I'm kinda new to Python and wasn't successful with my search so I hope that's not a duplicate.
I'm creating a list of exceptions that have to be handled by a function. This list contains dictionaries. If the index-key of the function corresponds to the index-key of the exception-list its values have to be used in the handling.
def main():
orig_destlist = getDestinations() #Contains all destinations in a list
destlist = []
func1(orig_destlist,destlist)
do_something_with_the_new_list(destlist)
def func1(orig_destlist,destlist):
exceptions = [];
exceptions.append({'index': 10, 'orig': "Departure", 'new': "Pending"})
exceptions.append({'index': 15, 'orig': "Pending", 'new': "Arrival"})
func2(orig_destlist,destlist,exceptions)
def func2(orig_destlist,destlist,exceptions):
for i in range (0,90):
#That's the command I want to figure out.
#If the dictionary list contains somewhere a key 'id'
#which hast the value of i
if any(i in 'index' for ex in exceptions):
destlist.append({'id': i, 'dest': ex['new']})
else: #If it doesn't contain the key, do the regular thing
destlist.append({'id': i, 'dest': orig_destlist[i]})
I am pretty aware that code is wrong. What is the proper way to find out if a certain key is existing in a list and use it in an if
?
That answer came up already: How can I check if key exists in list of dicts in python?
But it doesn't seem to handle the else
-part I require.