0

First of all i'm really sorry i shouldn't ask that question. i have a list of dictionary's and this dictionary holds keys values like:

dictList = [
            {
             0:1,
             1:1,
             2:None
             },
            {
             0:0,
             1:None,
             2:None
            },
           {
            0:0,
            1:1,
            2,1
           }
          ]

If none of the dictList's elements are none, I want to append it to another list. For example dictList[2] has not None value i want to append that dictionary to another list.

i tried to write it with flag but i think i have to create a flag array and check flagArray does not contains False like:

for i in range(len(dictList)):
dictLoop = lastCombination[i]
flagArray = []
for k,v in dictLoop.items():
    if(v is None):
        flagArray.append(False)
    else:
        flagArray.append(True)
    if(flagArray.__contains__(False)):
        showCombinationWithZero.append(dictList[i])
        
    else:
        showCombinationWithOutZero.append(dictList[i])

it didin't work well. Thanks for advices.

Ali Gökkaya
  • 408
  • 6
  • 20
F M
  • 5
  • 1

4 Answers4

5

You can use list comprehension to filter out dicts with None values. For example:

dictList = [{0:1,1:1,2:None},
           {0:0, 1:None, 2:None},
           {0:0,1:1,2:1}]

filtered = [d for d in dictList if all(v is not None for v in d.values())]
# [{0: 0, 1: 1, 2: 1}]

This just selects items from the list that meet the condition:

all(v is not None for v in d.values())

which is that all values are not None.

You can do the opposite for those with None:

[d for d in dictList if any(v is None for v in d.values())]

To do it in one loop, you can use the test as an index into a list of two lists:

dictList = [{0:1,1:1,2:None},
            {0:0, 1:None, 2:None},
            {0:0,1:1,2:1}]

lists = [[], []]

for d in dictList:
    i = all(v is not None for v in d.values())
    lists[i].append(d)

showCombinationWithZero, showCombinationWithoutZero = lists
# ([{0: 1, 1: 1, 2: None}, {0: 0, 1: None, 2: None}], [{0: 0, 1: 1, 2: 1}])
Mark
  • 90,562
  • 7
  • 108
  • 148
1

Here's a code, derived from yours, that contains two for loops:

  • The outer for loop goes through each dictionary in the list.
  • The inner for loop goes through each key and value pair to check for None

If any value is None, then contains_none value is set to True. So, this dictionary is ignored.

If all values are valid, then the variable will stay False and the dictionary will be added to a separate list.

dictList = [{0:1,1:1,2:None},
           {0:0, 1:None, 2:None},
           {0:0,1:1,2:1}]
           
good_dict = []
for d in dictList:
    contains_none = False
    for k, v in d.items():
        if v == None:
            contains_none = True
            
    if not contains_none:
        good_dict.append(d)
    else:
        print(f"{d} has None")
print(good_dict)
Alper
  • 63
  • 1
  • 5
0

You can use

nones = not all(i.values())

If all values are not None, nones would be set to False, else True.
With this you can make your code as following:

for i in range(len(dictList)):
    nones = not all(i.values())
    if not nones:
        showCombinationWithOutZero.append(dictList[i])
   else:
        showCombinationWithZero.append(dictList[i])
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • 1
    You should double check what `i` is in your last code example. Also, `all()` used like this will be a problem when some values are `0` as in the OP's data. – Mark Sep 04 '21 at 21:57
  • I saw this answer here https://stackoverflow.com/a/32308388/3485434 – Prophet Sep 04 '21 at 22:00
  • 1
    I would recommend trying to run it rather that copying it from another answer without trying it. It doesn't run. – Mark Sep 04 '21 at 22:03
0

You can get it with one line.

[x for x in dictList if all([not bool(v is None) for v in x.values()])]

Description

[not bool(v is None) for v in x.values()]
  1. Creates a list of bool values, False if value is None.
  2. Checks whether all values are True or not with all().
  3. Returns only if all values aren't None.
Minhyeoky
  • 1
  • 1
  • 2