{'INF_Test': ['mi-0e6d869b68d49c2c4', 0, 0], 'INF_Test2': [0, 'mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']}
need to be like output
{'INF_Test': ['mi-0e6d869b68d49c2c4'], 'INF_Test2': ['mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']}
CodePudding user response:
You can use a list comprehension and a dictionary comprehension:
{key: [item for item in value if item != 0] for key, value in data.items()}
This outputs:
{
'INF_Test': ['mi-0e6d869b68d49c2c4'],
'INF_Test2': ['mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']
}
CodePudding user response:
There are many ways to do this. Here's an opportunity to use filter():
d = {'INF_Test': ['mi-0e6d869b68d49c2c4', 0, 0], 'INF_Test2': [0, 'mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']}
e = {k : list(filter((0).__ne__, v)) for k, v in d.items()}
CodePudding user response:
aaa = {'INF_Test': ['mi-0e6d869b68d49c2c4', 0, 0], 'INF_Test2': [0, 'mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']}
for key in aaa.keys():
aaa[key] = list(filter(lambda num: num != 0, aaa[key]))
print(aaa)
Output
{'INF_Test': ['mi-0e6d869b68d49c2c4'], 'INF_Test2': ['mi-0a5d9089f493ae12a', 'mi-04506ea0d2810d3d5']}