Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Create List of Substrings from List of Strings in Python
Next article icon

Extract List of Substrings in List of Strings in Python

Last Updated : 09 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.

  • Using List Comprehension
  • Using the filter() Function
  • Using List Slicing
  • Using Regular Expressions
  • Using the map() Function

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.


Next Article
Create List of Substrings from List of Strings in Python

K

kasoti2002
Improve
Article Tags :
  • Python
  • Python Programs
  • substring
Practice Tags :
  • python

Similar Reads

  • Create List of Substrings from List of Strings in Python
    In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th
    3 min read
  • Extract Substrings From A List Into A List In Python
    Python is renowned for its simplicity and versatility, making it a popular choice for various programming tasks. When working with lists, one common requirement is to extract substrings from the elements of the list and organize them into a new list. In this article, we will see how we can extract s
    2 min read
  • Python - Filter list of strings based on the substring list
    The problem requires to check which strings in the main list contain any of the substrings from a given list and keep only those that match. Let us explore this problem and understand different methods to solve it. Using list comprehension with any() (Most Efficient)List comprehension is a concise a
    4 min read
  • Count Occurance of Substring in a List of Strings - Python
    To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list. Using sum() and Generator ExpressionThis method uses a generator
    3 min read
  • Python - Filter Strings combination of K substrings
    Given a Strings list, extract all the strings that are a combination of K substrings. Input : test_list = ["geeks4u", "allbest", "abcdef"], substr_list = ["s4u", "est", "al", "ge", "ek", "def"], K = 3 Output : ['geeks4u'] Explanation : geeks4u made up of 3 substr - ge, ek and s4u. Input : test_list
    4 min read
  • Convert list of strings to list of tuples in Python
    Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
    5 min read
  • Python | Convert List of String List to String List
    Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd
    6 min read
  • Python | Extract Nth words in Strings List
    Sometimes, while working with Python Lists, we can have problems in which we need to perform the task of extracting Nth word of each string in List. This can have applications in the web-development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using list compreh
    7 min read
  • Extract words starting with K in String List - Python
    In this article, we will explore various methods to extract words starting with K in String List. The simplest way to do is by using a loop. Using a LoopWe use a loop (for loop) to iterate through each word in the list and check if it starts with the exact character (case-sensitive) provided in the
    2 min read
  • Convert List of Tuples to List of Strings - Python
    The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings. For example, gi
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences