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)