Open In App

Extract List of Substrings in List of Strings in Python

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and commonly used methods to extract substrings from a list of strings in Python.

Extract List Of Substrings In List Of Strings In Python

Below, are the methods of how to Extract List Of Substrings In a List Of Strings In Python.

Extract List Of Substrings In List Of Strings Using List Comprehension

List comprehension is a concise and powerful way to create lists in Python. It can be employed to extract substrings based on certain conditions or patterns. The following example demonstrates how to extract all substrings containing a specific keyword:

Python3




string_list = ["apple", "banana", "cherry", "date"]
keyword = "an"
 
result = [substring for substring in string_list if keyword in substring]
print(result)


Output

['banana']

Extract List Of Substrings In List Of Strings Using the filter() Function

The filter() function is another elegant way to extract substrings based on a condition. In the example below, we use filter() in combination with a lambda function to find strings containing the letter ‘a’:

Python3




string_list = ["apple", "banana", "cherry", "date"]
 
result = list(filter(lambda x: 'a' in x, string_list))
print(result)


Output

['apple', 'banana', 'date']

Extract List Of Substrings In List Of Strings Using List Slicing

List slicing is a versatile technique that allows you to extract substrings based on their position within each string. The following example demonstrates how to extract the first three characters from each string in the list:

Python3




string_list = ["apple", "banana", "cherry", "date"]
 
result = [substring[:3] for substring in string_list]
print(result)


Output

['app', 'ban', 'che', 'dat']

Extract List Of Substrings In List Of Strings Using Regular Expressions

Regular expressions provide a powerful and flexible way to match and extract patterns from strings. The re module in Python facilitates working with regular expressions. The following example extracts substrings containing digits:

Python3




import re
 
string_list = ["apple123", "banana456", "cherry789", "date"]
 
result = [re.findall(r'\d+', substring) for substring in string_list]
print(result)


Output

[['123'], ['456'], ['789'], []]

Extract List Of Substrings In List Of Strings Using the map() Function

The map() function can be used to apply a specified function to each element of an iterable. In the example below, we use map() in conjunction with the str.split() method to extract the first word from each string in the list:

Python3




string_list = ["apple pie", "banana split", "cherry tart", "date cake"]
 
result = list(map(lambda x: x.split()[0], string_list))
print(result)


Output

['apple', 'banana', 'cherry', 'date']

Conclusion

Extracting substrings from a list of strings in Python can be achieved through various methods, each offering its own advantages based on specific requirements. Whether you prefer the simplicity of list comprehension, the elegance of the filter() function, the flexibility of regular expressions, or the versatility of list slicing and map(), Python provides a solution for every need. Choose the method that best suits your application and enhances your code readability and maintainability.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads