0

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.

Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • 4
    Please provide a *[mcve]*. Many of your variables aren't defined. Neither of your functions `return` anything. I advise you read up on how to write a Python function before implementing *any* logic. – jpp Apr 09 '18 at 10:47
  • 1
    [This is already asked,Please check it once](https://stackoverflow.com/questions/14790980/how-can-i-check-if-key-exists-in-list-of-dicts-in-python) – Gurram Madhu Apr 09 '18 at 10:49
  • 1
    `if key in dict` or `dict.get(key, default)` – Klaus D. Apr 09 '18 at 10:50
  • @OcasoProtal - I had this one open already but it doesn't handle the `else`-part – Qohelet Apr 09 '18 at 11:02
  • @jpp - The only not-defined variable is `destlist`, the only not-defined function `asgnment`. From both it's kinda clear what they are doing. There's not really a need to write an essay about what's going to happen after the magic is done. My problem is that I'm unable to set the line with `if any ...` right – Qohelet Apr 09 '18 at 11:10
  • @Qohelet, You're right, we don't ask for essays. What we do ask for is a *[mcve]*. – jpp Apr 09 '18 at 11:15
  • @jpp - better? If not let me know what you need to know – Qohelet Apr 09 '18 at 11:24
  • Your functions *still* don't `return` anything.. As such, your program does nothing. What's `self`. *Take the time to provide code that runs in isolation.* It's important because SO is a community-based website providing Q&A, not a code-writing service. – jpp Apr 09 '18 at 11:24
  • @jpp - self: https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self - removed it it's not relevant. Neither is what happens afterwards. I am unable to use the `any`-method properly and would like to know if in my case this is the method which is supposed to be used and if there is a method existing. My question is clear, why is it important what the rest of the code is doing? – Qohelet Apr 09 '18 at 11:31
  • @jpp - the rest of the code is not relevant to this question. Would it make you happy if I remove everything but the required line and write my question in a more verbose way? – Qohelet Apr 09 '18 at 11:34
  • Not sure if that's the best solution, it did the job: `e = dict((i['index'],i['new']) for i in exceptions) if i in e: destlist.append({'id': i, 'dest': e[1]}) else: destlist.append({'id': i, 'dest': orig_destlist[i]})` – Qohelet Apr 09 '18 at 12:20

1 Answers1

0

It is simple to check the keys

newl =  {'index': 10, 'orig': "Departure", 'new': "Pending", 'index2': 23}

for key in newl.keys():
  print(key)

Output

index
orig
new
index2

Now you can run your second function on the output.

MikiBelavista
  • 2,470
  • 10
  • 48
  • 70
  • That would be the approach I'd use in most programming languages. I just realized mostly Python has some kind of functionality that can handle such use-cases more efficiently. – Qohelet Apr 09 '18 at 11:07