-2

I would like to get only the first match if key is in dict. Else I would like to get a NaN. I'm struggling with the correct syntax to fit my needs. The match doesn't have to be in a list, I just need the number. I tried tons of variations...:

'Temperatur':[d2.get(key) for key in temperatur if key in d2][0] else None

'Temperatur':d2.get(key) for key in temperatur if key in d2 else None
  • 1
    Does this answer your question? [Why dict.get(key) instead of dict\[key\]?](https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey) – jrmylow Oct 02 '20 at 08:10

1 Answers1

2

You probably want next with a default:

'Temperatur': next((d2.get(key) for key in temperatur if key in d2), None)
tobias_k
  • 81,265
  • 12
  • 120
  • 179