Python | Segregate list elements by Suffix
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 | Append suffix/prefix to strings in list
- Python | Check if suffix matches with any string in given list
- Python | Replace elements in second list with index of same element in first list
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python | Add list elements with a multi-list based on index
- Python | Maximum sum of elements of list in a list of lists
- Python | Indices of sorted list of list elements
- Python | Segregate True and False value indices
- Python | Index of Non-Zero elements in Python list
- Python | Get last N elements from given list
- Python | Get first and last elements of a list
- Python | List frequency of elements
- Python | Absolute value of list elements
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.



