My list is ['text1', 'text2', 'text3']
so one so.
How can I extract only text1
or text2
?
Tried with re but can't figure out how to do that.
My list is ['text1', 'text2', 'text3']
so one so.
How can I extract only text1
or text2
?
Tried with re but can't figure out how to do that.
Given a list
my_list = ["a", "b", "c"]
You can access the elements in the list by specifying their index between square brackets
my_list[0] # "a"
my_list[1] # "b"
my_list[2] # "c"
you can filter by using the filter function with regular expression. Like this:
import re
mylist = ['text1', 'text2', 'text3']
r = re.compile("text2")
newlist = list(filter(r.match, mylist))
print(newlist)
result here:
['text2']
list_with_items = ["item1", "item2", "item3", "item4"]
list_with_search_items = ["item2", "item3"]
for item in list_with_search_items:
if item in list_with_items:
print(item)
output:
item2
item3
Its a simple list approach. If your list_with_items
and list_with_search_items
have the same length, then you can use the set approach as here: How can I compare two lists in python and return matches