I have the following list of dictionaries, that currently contain None values:
[{'connections': None}, {'connections': None}, {'connections': None}]
I want to loop through the list of elements, check if each "connections" key in each dictionary is None and return true if so. How can i check if all values are None?
CodePudding user response:
You can use a generator expression and all
to unpack all the dict values in lst
and check if they are all None:
out = all(x is None for d in lst for x in d.values())
Output:
True
CodePudding user response:
If you want a silent check when the condition is met:
dict_list = [{'connections': None}, {'connections': None}, {'connections': None}]
assert all( d['connections'] is None for d in dict_list), 'At least one connection value is not None'
If the condition is not met, an AssertionError
is raised and the message above will appear. Otherwise this line is go through.
CodePudding user response:
list =[{'connections': None}, {'connections': None}, {'connections':
None}]
for item in list:
if(item['connections']==None):
# ......You code