Python Dictionary - not in function

Check whether an item is not present in a Python Dictionary object with the not in operator

Dictionary Updated 10 Sep 2016 1 min read

key not in dict

Method Overview

  • The operator not in checks whether a given key is not present in a Python dictionary. If the key is not present returns TRUE else returns FALSE.
  • The not in operator behaves the opposite of the in operator.

Example

python
# Example Python progran that checks whether

            # a given key is present in a dictionary

environmentDictionary = {'path': '/bin:/opt/bin'}

if 'path' not in environmentDictionary:

                 print('Path is not set')

            else:

                 print('Path is set')

Output

Output
Path is set