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

 `# 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))` `# Segregating by Suffix``# using list comprehension + endwith()``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]` `# Printing result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Method #2: Using filter() + lambda + endwith()

This task can be performed using the filter function which performs a similar task internally as the above function and lambda is a helper function to build the logic.

Python3

 `# 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))` `# Segregating by Suffix``# using filter() + endwith() + lambda``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))` `# Printing result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time complexity: O(n), where n is the number of elements in the original list. This is because the filter function takes O(n) time to process the elements in the list, and the endswith() and lambda functions take O(1) time.
Auxiliary space: O(n), as it requires two lists of size n to store the result of the filter function.

Method #3: Without endswith() function

Python3

 `# Python3 code to demonstrate``# Segregating by Suffix` `# initializing list``test_list ``=` `[``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``]` `# initializing end Suffix``end_letter ``=` `'s'` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Segregating by Suffix``with_s ``=` `[]``without_s ``=` `[]``for` `i ``in` `test_list:``    ``if``(i[``-``1``]``=``=``end_letter):``        ``with_s.append(i)``    ``else``:``        ``without_s.append(i)` `# print result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using itertools.filterfalse() and lambda, filter()

This method uses the itertools.filterfalse() function, which returns elements from the input iterator for which the predicate is False, and the str.endswith() method to filter out all the strings that do not end with a particular letter. The left strings can be used to make a different list.

Python3

 `from` `itertools ``import` `filterfalse` `# Initializing list``test_list ``=` `[``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``]` `# Initializing end Suffix``end_letter ``=` `'s'` `# Printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Segregating by Suffix``# using itertools.filterfalse() and str.endswith()``with_s ``=` `list``(``filter``(``lambda` `x: x.endswith(end_letter), test_list))``without_s ``=` `list``(filterfalse(``lambda` `x: x.endswith(end_letter), test_list))` `# Printing result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))` `# This code is contributed by Edula Vinay Kumar Reddy`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time Complexity: O(n), where n is the number of elements in the list, because the itertools.filterfalse() function and the str.endswith() method are both O(n) operations.
Auxiliary Space: O(n), because two new lists with_s and without_s are created and each list has a size equal to the number of elements in the original list.

Method #5 : Using find() method

Python3

 `# Python3 code to demonstrate``# Segregating by Suffix` `# Initializing list``test_list ``=` `[``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``]` `# Initializing end Suffix``end_letter ``=` `'s'` `# Printing original list``print``(``"The original list : "` `+` `str``(test_list))`  `# Segregating by Suffix``with_s ``=` `[]``without_s ``=` `[]` `for` `i ``in` `test_list:``    ``if` `i.find(end_letter)``=``=``len``(i)``-``1``:``        ``with_s.append(i)``    ``else``:``        ``without_s.append(i)` `# Print result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #6: Using string slicing + for loop

Stepwise approach:

1. Initialize the list test_list and the end suffix end_letter.
2. Print the original list.
3. Initialize two empty lists with_s and without_s.
4. Loop through each element ‘i’ in test_list.
5. Check if the last letter of i is equal to end_letter using string slicing i[-1].
6. If it is, append i to with_s, else append it to without_s.
7. Print the two segregated lists.

Python3

 `# Python3 code to demonstrate``# Segregating by Suffix` `# Initializing list``test_list ``=` `[``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``]` `# Initializing end Suffix``end_letter ``=` `'s'` `# Printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Segregating by Suffix``with_s ``=` `[]``without_s ``=` `[]` `for` `i ``in` `test_list:``    ``if` `i[``-``1``] ``=``=` `end_letter:``        ``with_s.append(i)``    ``else``:``        ``without_s.append(i)` `# Printing result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))`

Output
```The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']```

Time complexity: O(N), where n is the length of the list test_list.
Auxiliary space: O(1), since we are only using a constant amount of space for the two empty lists with_s and without_s.

Method #7: Using Numpy

Algorithm:

1. Import the numpy library.
2. Create a numpy array of strings.
3. Specify the suffix that you want to segregate the array by.
4. Use the np.char.endswith() method to create a boolean mask of elements that end with the specified suffix.
5. Use this boolean mask to index the numpy array and create two new arrays: one containing all the strings
6. that end with the suffix, and one containing all the strings that do not end with the suffix.
7. Print the two new arrays.

Python3

 `import` `numpy as np` `# Initializing list as a numpy array``test_list ``=` `np.array([``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``])` `# Printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Initializing end Suffix``end_letter ``=` `'s'` `# Segregate by suffix ``# using numpy char module ``with_s ``=` `test_list[np.char.endswith(test_list, end_letter)]``without_s ``=` `test_list[~np.char.endswith(test_list, end_letter)]` `# Printing result``print``(``"The list without suffix s : "` `+` `str``(without_s))``print``(``"The list with suffix s : "` `+` `str``(with_s))` `# This code is contributed by Rayudu`

```Output:
The original list : ['apple' 'oranges' 'mango' 'grapes']
The list without suffix s : ['apple' 'mango']
The list with suffix s : ['oranges' 'grapes']```

Time complexity: O(n), where n is the number of strings in the input numpy array. This is because the np.char.endswith() method takes linear time to iterate over every string in the array and check if it ends with the specified suffix. The subsequent indexing of the array and creation of the two new arrays also takes linear time, as it involves iterating over every element in the array once.

Auxiliary Space: O(n), where n is the number of strings in the input numpy array. This is because the boolean mask and the two new arrays that are created all have the potential to contain every element in the original array. However, the actual space complexity will be less than O(n) if the input array contains many elements that do not end with the specified suffix, as these elements will not be included in the new arrays.

Method 8: Using regular expressions (re)

Python3

 `import` `re` `# Initializing list``test_list ``=` `[``'apple'``, ``'oranges'``, ``'mango'``, ``'grapes'``]``end_letter ``=` `'s'` `pattern ``=` `f``'.*{end_letter}\$'``without_s ``=` `[word ``for` `word ``in` `test_list ``if` `not` `re.match(pattern, word)]``with_s ``=` `[word ``for` `word ``in` `test_list ``if` `re.match(pattern, word)]` `# Printing answer``print``(``"The list without suffix s: "` `+` `str``(without_s))``print``(``"The list with suffix s: "` `+` `str``(with_s))`

Output
```The list without suffix s: ['apple', 'mango']
The list with suffix s: ['oranges', 'grapes']```

Time complexity: O(N), where n is the length of the test_list.
Auxiliary space: O(N), where n is the length of the test_list.

Previous
Next