I am trying to loop over a dictionary and call a function on each key.
If the function does not return None
entry for that key, I want the output to be appended to a list and I also want that key to be appended to a second list.
Here is my code:
output_list = []
key_list =[]
for i in dict.keys():
if obj.method(i):
output_list.append(obj.method(i))
key_list.append(i)
return output_list
return key_list
However, for some reason the second list, key_list
, is never populated - can you not have two statements below an if
like the above?
The reason that I am doing this is that I want to eventually produce an output where each key of the dict is listed alongside it's associated function ouput, whenever the output is not None
.