Extract Substrings From A List Into A List In Python
Last Updated :
16 Feb, 2024
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 substrings from a list into a list in Python.
Extract Substrings from a List into a List in Python
Below are some of the examples by which we can extract substrings from a list into a list in Python:
Example 1: Using List Comprehension
List comprehension is a concise and elegant way to create lists in Python. It also proves to be effective for extracting substrings from a list. This approach utilizes a compact syntax to iterate through the elements of the original_list and extracts the first three characters from each string. The resulting substring_list contains the desired substrings.
Python3
# Original List
original_list = ["apple", "banana", "cherry"]
# Extracting substrings using list comprehension
substring_list = [fruit[:3] for fruit in original_list]
# Displaying the results
print("Original List:", original_list)
print("Substring List:", substring_list)
OutputOriginal List: ['apple', 'banana', 'cherry']
Substring List: ['app', 'ban', 'che']
Example 2: Using Map and Lambda Function
This approach allows for more flexibility and customization in substring extraction. Here, the map function applies the lambda() function to each element of original_list, extracting the first three characters. The resulting substring_list is then converted to a list for better readability.
Python3
# Original List
original_list = ["python", "java", "csharp"]
# Extracting substrings using map and lambda
substring_list = list(map(lambda lang: lang[:3], original_list))
# Displaying the results
print("Original List:", original_list)
print("Substring List:", substring_list)
OutputOriginal List: ['python', 'java', 'csharp']
Substring List: ['pyt', 'jav', 'csh']
Conclusion
This article has explored the fundamental concepts of lists, substring extraction, and slicing in Python. Through practical examples and step-by-step guidance, you should now be equipped to confidently extract substrings from lists in your Python projects. Whether you're working with textual data or any other type of sequence, the ability to manipulate substrings is a valuable skill in your programming toolkit.
Similar Reads
Capitalize Each String in a List of Strings in Python In Python, manipulating strings is a common task, and capitalizing each string in a list is a straightforward yet essential operation. This article explores some simple and commonly used methods to achieve this goal. Each method has its advantages and use cases, providing flexibility for different s
3 min read
Read a CSV into list of lists in Python In this article, we are going to see how to read CSV files into a list of lists in Python. Method 1: Using CSV moduleWe can read the CSV files into different data structures like a list, a list of tuples, or a list of dictionaries.We can use other modules like pandas which are mostly used in ML appl
2 min read
How to Split a File into a List in Python In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples
5 min read
Python - Extract String elements from Mixed Matrix Given a Matrix, Extract all the elements that are of string data type. Input : test_list = [[5, 6, 3], ["Gfg", 3], [9, "best", 4]] Output : ['Gfg', 'best'] Explanation : All strings are extracted.Input : test_list = [["Gfg", 3], [9, "best", 4]] Output : ['Gfg', 'best'] Explanation : All strings are
6 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Python - Convert list of string to list of list In Python, we often encounter scenarios where we might have a list of strings where each string represents a series of comma-separated values, and we want to break these strings into smaller, more manageable lists. In this article, we will explore multiple methods to achieve this. Using List Compreh
3 min read