In Python, how to check whether a key already exists in a dict?

How to check whether a key already exists in a dict in Python since version 2.6?

If d is a dict(), you can directly test whether a key k is in d in Python by the in keyword like

if k in d:
  ..
Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *