How to extract elements of strings in a list based on the presence of a substring in python?

684 views Asked by At

I have a list which contains elements of strings. I am trying to capture the elements if it has specific substring into another variable by removing it in the original list.

Org_list = ["I am riding a bicycle to go to the movie", "He is riding a bike to go to the school", "She is riding a bicycle to go to the movie", "He is not riding a car to go to the school"]
substring1 = "riding a bike"
substring2 = "riding a car"

Now, I want to search the substrings in all the elements and remove the element which contains the substrings from the Org_list and capture the removed elements into another variable.

Desired Output:

Org_list = ["I am riding a bicycle to go to the movie", "She is riding a bicycle to go to the movie"]
New_variable = ["He is riding a bike to go to the school", "He is not riding a car to go to the school"]

I have tried this way:

res = list(filter(lambda x: all(y not in substring1 for y in x), Org_list))
res = list(filter(lambda x: all(y not in substring2 for y in x), Org_list))

The result I got for both the cases was [] which is obviously not what I except. Can someone provide me any clue about this?

3

There are 3 answers

0
InSync On BEST ANSWER

Using list comprehension:

def filter_by_substrings(Org_list, substrings):
  return [
    string for string in Org_list
    if any(substring in string for substring in substrings)
  ]

To break it down:

New_variable = []

# For each string in the original list
for string in Org_list:
  # We append it to the new list if at least one substring is found.
  if any(substring in string for substring in substrings):
    New_variable.append(string)

Try it:

Org_list = [
  "I am riding a bicycle",
  "He is riding a bike",
  "She is riding a bicycle",
  "He is not riding a car"
]

substrings = [
  "riding a bike",
  "riding a car"
]

New_variable = filter_by_substrings(Org_list, substrings)

print(New_variable)  # ['He is riding a bike', 'He is not riding a car']
0
matszwecja On

You're trying to use two different ways of solving the same problem together, which results in none of them working. filter already does a check for each element of the list, you don't need to do that again in the generator expression.

Filter solution:

res = list(filter(lambda x: (substring in x), Org_list))

List comprehension solution:

res = [x for x in Org_list if substring in x]
0
Marcin Wronecki On

To capture both list, you can use this code:

org_list = ["I am riding a bicycle", "He is riding a bike", "She is riding a bicycle", "He is not riding a car"]
substring1 = "riding a bike"
substring2 = "riding a car"
sub_list_1 = []

sub_list_2 = []
for elem in org_list:
    if substring1 not in elem and substring2 not in elem:
        sub_list_1.append(elem)
    else:
        sub_list_2.append(elem)

org_list = sub_list_1