When the code output occurs
if city_friends ['city'] ['title'] == City:
KEYERROR: 'CITY'
How to add a condition to me so that if this error appears, it’s just nothing to do and move to the next element of the dictionary
Answer 1, Authority 100%
More option:
if 'city' in city_friends and 'title' in city_friends ['city'] and city_friends ['city'] ['title'] == City:
Answer 2
arr = [
{'City': {'Name': 'London', 'Pop': 12}},
{}
]
for el in arr:
Try:
Print (EL ['City'])
Except:
Pass
# Either
for el in arr:
if 'city' in el:
Print (EL ['City'])
Answer 3
as option:
city = "hello world"
city_friends = {'city': {'title': 'Hello World'}}
_city = city_friends.get ('City')
If _city:
_title = _city.get ('Title')
ELSE: _TITLE = NONE
If _title and _title == city:
PRINT ("YES")