F
F
fumarys2021-07-20 20:37:06
Python
fumarys, 2021-07-20 20:37:06

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/'}]

Can you please tell me how to completely delete the lines where the mention of "? from =" occurs with the ability to expand the list of mentioned words?

An example of what should remain:
ps The number of lines can be completely different
[{'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

3 answer(s)
A
Alan Gibizov, 2021-07-20
@phaggi

a = [x for x in a if '?from' not in x['url']]

V
Vladimir Kuts, 2021-07-20
@fox_12

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/'}]

K
Kadabrov, 2021-07-20
@Kadabrov

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

spoiler
for i in enumerate(list):
    for  v in i[1].values():
        if '?from=' in v:
            list.pop(i[0])
print(list)

I'm most interested in how to arrange it in a list comprehension

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question