I have a problem. I have a list myList
inside these list, there a dictonaries. I want to count if the field dataOriginSystem
is empty or does not exist. Unfortunately I got the wrong result. if(key_nested == 'dataOriginSystem'): ... else: count = 1
The reason for this lies in the if-query. Since I am asking, does the field exist? If no, then count it up and since I loop through all the nested keys, one of the errors is here. Also, is there a way to make this more efficient?
How can I query how many of the field dataOriginSystem
are empty or non-existent?
count = 0
for element in myList:
for key in element.keys():
if(key == 'metaData'):
for key_nested in element[key].keys():
if(key_nested == 'dataOriginSystem'):
if(key_nested == None):
count = 1
else:
count = 1
print(count)
myList = [
{'_id': 'orders/213123',
'contactEditor': {'name': 'Max Power',
'phone': '1234567',
'email': '[email protected]'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': '[email protected]'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': 'Goods',
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
'contactEditor': {'name': 'Max Power2',
'phone': '1234567',
'email': '[email protected]'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': '[email protected]'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': 'Goods',
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
'contactEditor': {'name': 'Max Power2',
'phone': '1234567',
'email': '[email protected]'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': '[email protected]'},
'isCompleteDelivery': False,
'metaData': {
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
'contactEditor': {'name': 'Max Power2',
'phone': '1234567',
'email': '[email protected]'},
'contactSoldToParty': {'name': 'Max Not',
'phone': '123456789',
'email': '[email protected]'},
'isCompleteDelivery': False,
'metaData': {'dataOriginSystem': None,
'dataOriginWasCreatedTime': '10:12:12',},
'orderDate': '2021-02-22',
'orderDateBuyer': '2021-02-22',
},
]
Result should be
[OUT] 2
# Because of the two last elements.
# The first element does not exist
# and the second ist None.
CodePudding user response:
You can use dict.get
directly on the nested key, returning a default value of None
, and then count the number of None
values you get:
sum(d['metaData'].get('dataOriginSystem', None) is None for d in myList)
Output
2
CodePudding user response:
You don't have to loop through the keys. Access the item you want directly and increment the counter if the item is not found, or found but None.
count = 0
for element in myList:
if element["metaData"].get("dataOriginSystem", None) is None:
count = 1
print(count)
CodePudding user response:
You can try something like this...
non_existant = len([0 for item in myList if item['metaData'].get('dataOriginSystem') == None or item['metaData'].get('dataOriginSystem') == ''])
print(non_existant)
Output...
2