Reputation: 17
I have a dictionary with lists of lists. I would like sublists that have a certain ''
element to be removed.
Below I put how I'm trying to forward this. I'm having trouble removing the entire list. In the code below the element ''
has been removed
d = {'af':[['info01','info02'],['info03','info04'],['info05','']]}
print(d['af'])
af2 = any('' in sublist for sublist in d['af'])
af_final = [[element for element in sub if element != ''] for sub in d['af']]
if af2:
d['af'] = af_final
print(af_final)
[['info01', 'info02'], ['info03', 'info04'], ['info05', '']]
[['info01', 'info02'], ['info03', 'info04'], ['info05']]
[['info01', 'info02'], ['info03', 'info04']]
Upvotes: 0
Views: 69
Reputation: 3927
It sounds like you are looking for something like this:
d = {
'af':[
['info01', 'info02'],
['info03', 'info04'],
['info05', ''],
]
}
print(d['af'])
# [['info01', 'info02'], ['info03', 'info04'], ['info05', '']]
af2 = any('' in sublist for sublist in d['af'])
af_final = [sublist for sublist in d['af'] if '' not in sublist]
if af2:
d['af'] = af_final
print(af_final)
# [['info01', 'info02'], ['info03', 'info04']]
Upvotes: 2