python - the calculation conditions IF in dictionaray -


i don't understand calculation condition if if dictionary :-

why when code

dlist = [{'bilbo':'ian','frodo':'elijah'} , {'bilbo':'martin','thorin':'richard'}]  k = 'bilbo'  [ ks[k] if k in ks else 'not present' ks in dlist] 

but when wrote code

[ ks[k]  ks in dlist if k in ks else 'not present' ]  

i syntaxerror: invalid syntax

[ks[k] if k in ks else 'not present' ks in dlist]     

implies

[(ks[k] if k in ks else 'not present') ks in dlist]  

your proposal of

[ ks[k]  ks in dlist if k in ks else 'not present' ] 

cannot have parentheses placed implicitly in sensible way. here various ways of grouping syntax. none of them intended:

[(ks[k] ks in dlist) if k in ks else 'not present'] [ks[k] ks in (dlist if k in ks else 'not present')] [ks[k] ks in dlist if k in ks] 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -