Python | Split String and Remove Last Element

Rate this post

🍎Summary: Split the string using rsplit with the specified separator and maxsplit as 1. This ensures the string is split from the right, and then you can extract the first value from this list which is the required output string.

Minimal Example

text = '21-12-1994'

# Method 1
res = text.rsplit('-', 1)[0]
print(res)

# Method 2.1
print('-'.join(text.split('-')[:-1]))

# Method 2.2
res = text.split('-')
res.pop()
result = '-'.join(res)
print(result)

# Method 3
res = text[:text.rfind('-')]
print(res)

# Method 4
import re
print('-'.join(re.findall(r'[^-]+', text)[:-1]))

# OUTPUTS in each case: 
# 21-12

Problem Formulation

πŸ“œProblem: How will you split a string and eliminate the last item from the split string list and then join the items to create a new string?

Follow the example given below to understand what the question demands.

Example

# Input
text = 'abc-pqr-lmn-xyz'

# Expected Output:
abc-pqr-lmn

❗Note the last substring (xyz) has been removed from the original string.


Method 1: rsplit and Extract Using by Index

✍️Approach: Use rsplit to split the string using hyphen as the separator and 1 as the maxsplit value. This ensures that the string is split only once from the right. Thus, all that remains to be done is to eliminate the last item from the split string list. In other words, you can simply grab the first item from the split string list, which will yield you the output that you desire.

Code:

text = 'abc-pqr-lmn-xyz'
res = text.rsplit('-', 1)[0]
print(res)

# abc-pqr-lmn

Method 2.1: Split and Join Using a List Comprehension

✍️Approach: Split the string using the split function and eliminate the last item by using list slicing such that the start index is the first item of the list while the end index is the last item. Since when you slice a list, the end index is not included. Hence, the item at the end will be automatically excluded from the list. Finally, join the items of the list into a single string using the join function with the separator that was previously used to split the string(hyphen in this case).

Code:

text = 'abc-pqr-lmn-xyz'
print('-'.join(text.split('-')[:-1]))

# abc-pqr-lmn

Method 2.2: Split, Pop and Join

✍️Approach: The idea here is to split the string using the separator with the help of the split method. Then, eliminate the last item from the list returned by the split function using the list pop() method. Finally, join the items of the list into a single string using the join method.

Code:

text = 'abc-pqr-lmn-xyz'
res = text.split('-')
res.pop()
result = '-'.join(res)
print(result)

# abc-pqr-lmn

Method 3: Using rfind

✍️Approach: Use the rfind method to find the last index of the separator in the given string. Use this index to slice the string. This means you are effectively slicing the string from the start until the last occurrence of the separator, after which the last substring that needs to be eliminated appears. Therefore, slicing the string this way ensures that the last substring is automatically excluded from the string.

Code:

text = 'abc-pqr-lmn-xyz'
res = text[:text.rfind('-')]
print(res)

# abc-pqr-lmn

Method 4: Using re.findall

✍️Approach: Use re.findall to match all the strings separated by hyphen. Once the split list is ready, you can eliminate the last item from this list using list slicing like so [:-1]. This ensures that the last item gets eliminated as the string will be sliced from the start index (included) until the last index specified (excluded) in the slice notation. -1 indicates the index of the last item; therefore, it will not be included. Finally, join the items of this list and create a new string using the join method, which takes a hyphen as the joining string.

Code:

import re

text = 'abc-pqr-lmn-xyz'
print('-'.join(re.findall(r'[^-]+', text)[:-1]))

# abc-pqr-lmn

Conclusion

That was it for this article. I hope now you can easily split a string and remove the last item from the list using any of the methods mentioned in this tutorial. Please subscribe and stay tuned for more interesting solutions and discussions in the future. Happy coding!