Python - Filter list elements starting with given Prefix Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list we need to filter list elements that are starting with given prefix. For example, a = ['apple', 'banana', 'avocado', 'apricot', 'cherry'] and given prefix is p = 'ap' we need to filter all the list elements that are starting with given prefix so that output should be ['apple', 'apricot'].Using List ComprehensionList comprehension iterates through each element in the list and checks if it starts with given prefix using startswith(). If the condition is met element is included in new filtered list. Python a = ['apple', 'banana', 'avocado', 'apricot', 'cherry'] p = 'ap' # Filter elements starting with the given prefix using list comprehension b = [word for word in a if word.startswith(p)] print(b) Output['apple', 'apricot'] Explanation:List comprehension iterates through each word in the list a and checks if it starts with the prefix p using the startswith() method.Words that meet the condition are added to the new list f which contains the filtered elements.Using filter() with lambdafilter() function filters elements from the list based on the condition defined in the lambda function which checks if each element starts with given prefix result is an iterator which is converted into a list for further use. Python a = ['apple', 'banana', 'avocado', 'apricot', 'cherry'] p = 'ap' # Filter elements starting with the given prefix using filter() and lambda b = list(filter(lambda word: word.startswith(p), a)) print(b) Output['apple', 'apricot'] Explanation:filter() function applies the lambda function to each word in the list a, checking if the word starts with the prefix p using startswith().Result of filter() is an iterator which is converted to a list storing the words that match the condition in f.Using for LoopA for loop iterates through each element in the list checking if it starts with the given prefix using startswith(). If condition is met element is appended to a new list containing filtered words. Python a = ['apple', 'banana', 'avocado', 'apricot', 'cherry'] p = 'ap' # Filter elements starting with the given prefix using a for loop b = [] for word in a: if word.startswith(p): b.append(word) print(b) Output['apple', 'apricot'] Explanation:for loop iterates through each word in the list a checking if it starts with prefix p using startswith().If condition is satisfied word is appended to list f which stores filtered elements. Comment More infoAdvertise with us Next Article Python | Filter String with substring at specific position M manjeet_04 Follow Improve Article Tags : Python Python list-programs Practice Tags : python Similar Reads Filter Python list by Predicate in Python A predicate is a function that returns either True or False for a given input. By applying this predicate to each element of a list, we can create a new list containing only the elements that satisfy the condition. Let's explore different methods to filter a list based on a predicate.Using list comp 3 min read How to remove lines starting with any prefix using Python? Given a text file, read the content of that text file line by line and print only those lines which do not start with a defined prefix. Also, store those printed lines in another text file. There are the following ways in Python in which this task can be done Program to remove lines starting with an 4 min read Python | Filter String with substring at specific position Sometimes, while working with Python string lists, we can have a problem in which we need to extract only those lists that have a specific substring at a specific position. This kind of problem can come in data processing and web development domains. Let us discuss certain ways in which this task ca 7 min read Python - Filter the List of String whose index in second List contains the given Substring Given two lists, extract all elements from the first list, whose corresponding index in the second list contains the required substring. Examples: Input : test_list1 = ["Gfg", "is", "not", "best", "and", "not", "CS"], test_list2 = ["Its ok", "all ok", "wrong", "looks ok", "ok", "wrong", "thats ok"], 10 min read Python - Remove empty strings from list of strings When working with lists of strings in Python, you may encounter empty strings (" ") that need to be removed. We'll explore various methods to Remove empty strings from a list. Using List ComprehensionList comprehension is the most concise and efficient method to filter out empty strings. This method 2 min read Python - Remove empty strings from list of strings When working with lists of strings in Python, you may encounter empty strings (" ") that need to be removed. We'll explore various methods to Remove empty strings from a list. Using List ComprehensionList comprehension is the most concise and efficient method to filter out empty strings. This method 2 min read Like