Ascendant
0
Q:

check dictionary has key python

if word in data:
  return data[word]
else:
  return "The word doesn't exist. Please double check it."
9
dict = {"key1": 1, "key2": 2}

if "key1" in dict:
3
d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("this will execute")

if "nonexistent key" in d:
    print("this will not")
0
d = {"key1": 10, "key2": 23}

if "key1" in d:
    print("this will execute")

if "nonexistent key" in d:
    print("this will not")
10
>>> lod = [{1: "a"}, {2: "b"}]
>>> any(1 in d for d in lod)
True
>>> any(3 in d for d in lod)
False
0

New to Communities?

Join the community