Given that I'm new in Python and my background is in social sciences I I'll try to do my best to explain the problem and I thank you in advance for the valuable help!
I have a list of dictionaries storing information of online recipes. For the specific key ['ingredients'], I have a list of strings as value. I would like to clean the list by deleting the empty strings.
Here is an example of how my data looks like:
data = [{'title': 'Simple Enchiladas Verdes',
'prep_time': '15 min',
'cook_time': '30 min',
'ingredients': ['', 'chicken', '','','','tomato sauce']
},
{...}, {...}]
The result that I would like to obtain for the key 'ingredients' is:
data = [{'title': 'Simple Enchiladas Verdes',
'prep_time': '15 min',
'cook_time': '30 min',
'ingredients': ['chicken','tomato sauce']
},
{...}, {...}]
I have tried different codes:
for dct in data:
for lst in dct['ingredients']:
for element in lst:
if element == '':
dct['ingredients'] = lst.remove(element)
for dct in current_dict:
for lst in dct['ingredients']:
dct['ingredients'] = list(filter(lambda x: x!=''))
for dct in data:
for lst in dct['ingredients']:
for x in lst:
if x == "":
dct['ingredients'] = lst.remove(x)
But none of them solve my problem.
filter().