Issue
I am new to python. I am trying to create a new list based on a specific condition that is, fruits['type'] === 'edit'
fruits = [{'type': 'edit',
'ns': 0,
'title': 'List',
'pageid': 39338740},
{'type': 'new',
'ns': 0,
'title': 'John Braid',
'pageid': 8164456},
{'type': 'edit',
'ns': 0,
'title': 'lokan',
'pageid': 65869267}]
my code returns an empty array:
newlist = []
for x in fruits:
if x['type'] == 'edit' in x:
newlist.append(x)
print(newlist)
Solution
fruits = [{'type': 'edit',
'ns': 0,
'title': 'List',
'pageid': 39338740},
{'type': 'new',
'ns': 0,
'title': 'John Braid',
'pageid': 8164456},
{'type': 'edit',
'ns': 0,
'title': 'lokan',
'pageid': 65869267}]
newlist = [fruit for fruit in fruits if fruit["type"] == "edit"]
print(newlist)
Answered By - Dmitry Erohin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.