getting a substring from each element of a list

  • Last Update :
  • Techknowledgy :

Method #1 : Using list comprehensionList comprehension is an elegant way to perform any particular task as it increases readability in a long run. This task can be performed using naive method and hence can be reduced to list comprehension as well., Method #2 : Using filter() + lambdaThis function can also perform this task of finding the strings with the help of lambda. It just filters out all the strings matching the particular substring and then adds it in a new list., Method #3 : Using re + search()Regular expressions can be used to perform many task in python. To perform this particular task also, regular expressions can come handy. It finds all the matching substring using search() and returns result.,The classical problem that can be handled quite easily by Python and has been also dealt with many times is finding if a string is substring of other. But sometimes, one wishes to extend this on list of strings, and hence then requires to traverse the entire container and perform the generic algorithm.

The original list is: ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
All strings with given substring are: ['GeeksforGeeks', 'Geeky']

Suggestion : 2
1._
print[s
   for s in list
   if sub in s]

If you want them separated by newlines:

print "\n".join(s
   for s in list
   if sub in s)

Full example, with case insensitivity:

mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'

print "\n".join(s
   for s in mylist
   if sub.lower() in s.lower())

All the answers work but they always traverse the whole list. If I understand your question, you only need the first match. So you don't have to consider the rest of the list if you found your first match:

mylist = ['abc123', 'def456', 'ghi789']
sub = 'abc'
next((s
   for s in mylist
   if sub in s), None) # returns 'abc123'

If the match is at the end of the list or for very small lists, it doesn't make a difference, but consider this example:

import timeit

mylist = ['abc123'] + ['xyz123'] * 1000
sub = 'abc'

timeit.timeit('[s for s in mylist if sub in s]', setup = 'from __main__ import mylist, sub', number = 100000)
#
for me 7.949463844299316 with Python 2.7, 8.568840944994008 with Python 3.4
timeit.timeit('next((s for s in mylist if sub in s), None)', setup = 'from __main__ import mylist, sub', number = 100000)
#
for me 0.12696599960327148 with Python 2.7, 0.09955992100003641 with Python 3.4

Use a simple for loop:

seq = ['abc123', 'def456', 'ghi789']
sub = 'abc'

for text in seq:
   if sub in text:
   print(text)

yields

abc123

I'd just use a simple regex, you can do something like this

import re
old_list = ['abc123', 'def456', 'ghi789']
new_list = [x
   for x in old_list
   if re.search('abc', x)
]
for item in new_list:
   print item

This prints all elements that contain sub:

for s in filter(lambda x: sub in x, list): print(s)

Suggestion : 3

September 8, 2021April 20, 2022

Simple example code. A list contains a substring if any element in the list contains that substring. For example, the list contains "AC" because "AC" is a substring of "BAC".

lst = ["ABC", "BAC", "CAB"]
str1 = "AC"

res = any(str1 in string
   for string in lst)

print(res)

Use a list comprehension, this way construct a new list containing each element that contains the substring.

lst = ["ABC", "BAC", "CAB"]
str1 = "AC"

res = [string
   for string in lst
   if str1 in string
]

print(res)