Answer the question
In order to leave comments, you need to log in
How to remove a line from list when mentioned from a list of words?
Hello! Faced the following problem. There is a list with the following information:
[{'name': 'Arsenal', 'manager': 'Mikel Arteta', 'url': 'https://site.com/clubs/1030/?from=c_london'
{'name': 'Aston Villa', 'manager': 'Dean Smith', 'url': 'https://site.com/clubs/1040/'
{'name': 'Brentford', 'manager': 'Thomas Frank', 'url': 'https://site.com/clubs/1050/?from=city_london'
{'name': 'Brighton and Hove Albion', 'manager': 'Graham Potter', 'url': 'https://site.com/clubs/1020/'}]
[{'name': 'Aston Villa', 'manager': 'Dean Smith', 'url': 'https://site.com/clubs/1040/'
{'name': 'Brighton and Hove Albion', 'manager': 'Graham Potter', 'url': 'https://site.com/clubs/1020/'}]
Answer the question
In order to leave comments, you need to log in
a = [
{'name': 'Arsenal', 'manager': 'Mikel Arteta', 'url': 'https://site.com/clubs/1030/?from=c_london'},
{'name': 'Aston Villa', 'manager': 'Dean Smith', 'url': 'https://site.com/clubs/1040/'},
{'name': 'Brentford', 'manager': 'Thomas Frank', 'url': 'https://site.com/clubs/1050/?from=city_london'},
{'name': 'Brighton and Hove Albion', 'manager': 'Graham Potter', 'url': 'https://site.com/clubs/1020/'}
]
a = list(filter(lambda x:'?from' not in x['url'], a))
print(a)
# [{'name': 'Aston Villa', 'manager': 'Dean Smith', 'url': 'https://site.com/clubs/1040/'},
# {'name': 'Brighton and Hove Albion', 'manager': 'Graham Potter', 'url': 'https://site.com/clubs/1020/'}]
First you fix the list, which in the example,
then you do a for for the list using enumerate(list)
you do a second for to unpack the dictionary, you check with if for the presence of the text "?from=" in the value of the dictionary
, if the condition is true, then delete it from the list with the help of list. pop(index of the line) the whole line
if you are not interested to understand yourself under the spoiler code
for i in enumerate(list):
for v in i[1].values():
if '?from=' in v:
list.pop(i[0])
print(list)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question