-1

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?

Mark R
  • 137
  • 1
  • 8
  • Does this answer your question? [How to check if all elements of a list match a condition?](https://stackoverflow.com/questions/10666163/how-to-check-if-all-elements-of-a-list-match-a-condition) (the condition being `d['connections'] is None` for each dictionary `d`) – mkrieger1 May 10 '22 at 09:20

5 Answers5

1

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
  • This doesn't specifically check if "connections" is None, it checks if *all* values in each dictionary are None (from the wording of the question it is not very clear if this distinction is relevant). – mkrieger1 May 10 '22 at 09:21
  • I edited the main question, hopefully it is clearer. But yes in this case all have to be None. – Mark R May 10 '22 at 09:29
0
list =[{'connections': None}, {'connections': None}, {'connections': 
None}]
for item in list:
if(item['connections']==None):
    return True

the for loop here will get every element ,if statement will check value of each element's key in array,so if its None will return True.

mahmoud2020
  • 97
  • 1
  • 7
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 10 '22 at 12:40
  • thank you very much .i will do that next time – mahmoud2020 May 10 '22 at 13:46
0

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.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
0

Here is the function where you can pass the list of dictionaries to check dictionaries contained all None values.

def func(list):
    for dict in list:
        for key in dict:
            if dict[key] is not None:
                return False
    
    return True
print(func([{'connections': None}, {'connections': None}, {'connections': None}]))
  • 1
    While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ – Pranav Hosangadi May 10 '22 at 21:29
0

Simply you can use key 'connections' from that dict with all using list compression

lst = [{'connections': None}, {'connections': None}, {'connections': None}]

if all([di.get('connections') == None for di in lst]):
    print("All dict connections value None value")
else:
    print("Some dict connections have not None value")