Hunter
Hunter

Reputation: 357

Python: Looping over all the lists in a dictionary simultaneously in order to filter out elements

I have some dictionary

someDict = {
    'foo1': [1, 4, 7, 0, -2],
    'foo2': [0, 2, 5, 3, 6],
    'foo3': [1, 2, 3, 4, 5]
}

I would like to loop over all the elements in each list with Python 3, and when the element at some given index equals zero, I want to delete that element at that index for all the lists/properties in the dictionary. So that the dictionary ends up as

someDict = {
    'foo1': [4, 7, -2],
    'foo2': [2, 5, 6],
    'foo3': [2, 3, 5]
}

Please note that I don't know beforehand how many keys/lists the dictionary will have, and I don't know how many elements the list will contain. I have come up with the following code, which seems to work, but was wondering if there is a more efficient way to do this?

keyPropList = someDict.items()
totalList = []

for tupleElement in keyPropList:
    totalList.append(tupleElement[1])

copyTotalList = totalList[:]
for outerRow in copyTotalList:
    for outerIndex, outerElement in enumerate(outerRow):
        if outerElement==0:
            for innerIndex, _ in enumerate(copyTotalList):
                del totalList[innerIndex][outerIndex]

print('someDict =', someDict)

Upvotes: 2

Views: 110

Answers (3)

iGian
iGian

Reputation: 11193

Other option, more verbose and destructive (pop) for original_dict:

some_dict = {
    'foo1': [1, 0, 2, 3, -2],
    'foo2': [0, 2, 5, 3, 6],
    'foo3': [1, 2, 3, 0, 5]
}

for k, v in some_dict.items():
  id = []
  for i, e in enumerate(v):
    if e == 0: id.append(i-len(id))
  [ [ some_dict[k].pop(x) for x in id ] for k,v in some_dict.items() ]

print(some_dict)
#=> {'foo1': [2, -2], 'foo2': [5, 6], 'foo3': [3, 5]}

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71471

You can find a list of "banned" indices which can then be used to filter the structure:

someDict = {
  'foo1': [1, 4, 7, 0, -2],
  'foo2': [0, 2, 5, 3, 6],
  'foo3': [1, 2, 3, 4, 5]
}
results = {i for c in someDict.values() for i, a in enumerate(c) if not a}
new_dict = {a:[c for i, c in enumerate(b) if i not in results] for a, b in someDict.items()}

Output:

{'foo1': [4, 7, -2], 'foo2': [2, 5, 6], 'foo3': [2, 3, 5]}

Upvotes: 8

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96246

Here's a cheeky one-liner:

>>> dict(zip(someDict, map(list, zip(*(ts for ts in zip(*someDict.values()) if all(ts))))))
{'foo3': [2, 3, 5], 'foo1': [4, 7, -2], 'foo2': [2, 5, 6]}
>>>

Upvotes: 3

Related Questions