When it is required to find the selective consecutive suffix join, a simple iteration, the ‘endswith’ method and the ‘append’ method can be used.
Example
Below is a demonstration of the same
my_list = ["Python-", "fun", "to-", "code"] print("The list is :") print(my_list) suffix = '-' print("The suffix is :") print(suffix) result = [] temp = [] for element in my_list: temp.append(element) if not element.endswith(suffix): result.append(''.join(temp)) temp = [] print("The result is :") print(result)
Output
The list is : ['Python-', 'fun', 'to-', 'code'] The suffix is : - The result is : ['Python-fun', 'to-code']
Explanation
- A list of strings is defined and is displayed on the console.
- A value for suffux is defined and is displayed on the console.
- Two empty lists are created.
- The list is iterated over, and the elements are appended to the empty list.
- If the element doesn’t end with the specific suffix, it is appended to the empty list using ‘join’ method.
- The other list is again emptied.
- This is displayed as output on the console.