Sometimes, we need to filter the list with the last character of each string in the list. This type of task has many applications in day-day programming and even in the competitive programming domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + endswith()
In this method, we use list comprehension for traversal logic and the endswith method to filter out all the strings that end with a particular letter. The left strings can be used to make a different list.
# Python3 code to demonstrate # Segregating by Suffix # Using list comprehension + endwith() # initializing list test_list = ['apple', 'oranges', 'mango', 'grapes'] # initializing end Suffix end_letter = 's' # printing original list print("The original list : " + str(test_list)) # using list comprehension + endwith() # Segregating by Suffix with_s = [x for x in test_list if x.endswith(end_letter)] without_s = [x for x in test_list if x not in with_s] # print result print("The list without suffix s : " + str(without_s)) print("The list with suffix s : " + str(with_s)) |
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']
Method #2 : Using filter() + lambda + endwith()
This task can be performed using the filter function with performs the similar task internally as the above function and lambda is helper function to build the logic.
# Python3 code to demonstrate # Segregating by Suffix # Using filter() + endwith() + lambda # initializing list test_list = ['apple', 'oranges', 'mango', 'grapes'] # initializing end Suffix end_letter = 's' # printing original list print("The original list : " + str(test_list)) # using filter() + endwith() + lambda # Segregating by Suffix with_s = list(filter(lambda x: x.endswith(end_letter), test_list)) without_s = list(filter(lambda x: not x.endswith(end_letter), test_list)) # print result print("The list without suffix s : " + str(without_s)) print("The list with suffix s : " + str(with_s)) |
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']
Recommended Posts:
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python - Segregate elements by delimiter
- Python | Segregate True and False value indices
- Python | Segregate Positive and Negative Integers from mixed string
- Python | Check if suffix matches with any string in given list
- Python | Append suffix/prefix to strings in list
- Python - Suffix List Sum
- Python - Remove suffix from string list
- Python - Suffix Product in list
- Python | Check Numeric Suffix in String
- Python | Merging two strings with Suffix and Prefix
- Python - Find Keys with specific suffix in Dictionary
- Python | Extract suffix after K
- Python - Substring Suffix Frequency
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- PyQt5 QSpinBox - Accessing Suffix
- PyQt5 QSpinBox - Setting Suffix
- PyQt5 QDoubleSpinBox – Getting Suffix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

