Remove obejcts from list if specific characters are included | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Remove obejcts from list if specific characters are included

Hi, I have a list with links and I want to remove links that include specific words or characters. Example: link_list = ["www.hello.com", "www.howareyou?.com", "www.example.com", "www.???.com"] Now I want to remove every link that includes a question mark so that my outcome is: Result example: link_list = ["www.hello.com", "www.example.com"] I can't find my solution in the normal commands like .pop or .remove. Whenever I try I have to specify exactly which link I want to remove like: link_list.remove("www.howareyou?.com") But I don't want to be this specific because I have too many links. I just want to remove all of the links that include a specific word or character. I'm sorry for my English but I hope it's understandable what I mean. Thank you in advance.

18th Jan 2021, 8:48 PM
🌱Zuhal🐛🌿
🌱Zuhal🐛🌿 - avatar
5 Answers
18th Jan 2021, 9:05 PM
Oma Falk
Oma Falk - avatar
+ 2
in other languages u would filter.
18th Jan 2021, 9:06 PM
Oma Falk
Oma Falk - avatar
+ 2
Frogged You actually can just as easily use filter() in python. In your code change the line for ll2 to: ll2 = list(filter(lambda link: badword not in link, link_list))
19th Jan 2021, 3:39 AM
ChaoticDawg
ChaoticDawg - avatar
0
You can use regex but you can easly use iteration with for loop and remove this particular substring from each list element for link in link_list : if "?" in link: link.remove("?")
18th Jan 2021, 9:06 PM
HBhZ_C
HBhZ_C - avatar
0
ChaoticDawg yes...but that list comprehension is said to be more pythonic .
19th Jan 2021, 5:25 AM
Oma Falk
Oma Falk - avatar