Python – Avoid Last occurrence of delimiter
Last Updated :
05 Apr, 2023
Given an Integer list, perform join with the delimiter, avoiding the extra delimiter at the end.
Input : test_list = [4, 7, 8, 3, 2, 1, 9], delim = “*”
Output : 4*7*8*3*2*1*9
Explanation : The rear “*” which usually occurs in concatenation, is avoided.
Input : test_list = [4, 7, 8, 3], delim = “*”
Output : 4*7*8*3
Explanation : The rear “*” which usually occurs in concatenation, is avoided.
Method #1: Using String slicing
Use string slice to slice off the last character from the string after forming.
Step by step approach :
- A delimiter called “delim” is initialized with the value “$”.
- A string called “res” is initialized to an empty string.
- A for loop is used to iterate through each element in the “test_list” and append the string representation of the element with the delimiter “$” to the “res” string. This creates a joined string with the delimiter “$” separating each element in the list. However, this approach will leave a stray “$” at the end of the joined string.
- The last occurrence of the delimiter “$” is removed from the “res” string using slicing. This is done by assigning the string “res” to a slice that excludes the last character of the string, which is the stray “$”.
- Finally, the joined string without the last occurrence of the delimiter “$” is printed using the print() function and concatenation with the str() function.
Below is the implementation of the above approach:
Python3
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The original list is : " + str (test_list))
delim = "$"
res = ''
for ele in test_list:
res + = str (ele) + "$"
res = res[: len (res) - 1 ]
print ( "The joined string : " + str (res))
|
Output
The original list is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$9
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method #2 : Using map() + join() + str()
In this, we completely avoid loop method to solve this problem, and employ map() to convert to string and join() to perform task of join.
Python3
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The original list is : " + str (test_list))
delim = "$"
res = delim.join( map ( str , test_list))
print ( "The joined string : " + str (res))
|
Output
The original list is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$9
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using str.join() with list comprehension
We can use a list comprehension to convert each element of the list to a string and then use the str.join() method to join the elements with the delimiter. Finally, we can remove the last occurrence of the delimiter using string slicing.
Python3
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The original list is : " + str (test_list))
delim = "$"
res = delim.join([ str (ele) for ele in test_list])
res = res[: - 1 ]
print ( "The joined string : " + str (res))
|
Output
The original list is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$
Time complexity: The time complexity of the given program is O(n), where n is the length of the input list.
Auxiliary space: The auxiliary space used by the program is O(n), where n is the length of the input list.
Method #4: Using reduce() and lambda function:
Step-by-Step Approach:
- The input list is initialized as test_list.
- The original list is printed using the print() function.
- The delimiter is initialized as $
- The reduce() function from functools module is used to apply the lambda function to the list of integers, where the lambda function concatenates each element of the list with the delimiter in between.
- The resulting string is assigned to the variable res.
- The final result is printed using the print() function.
Python3
from functools import reduce
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The Original List is : " + str (test_list))
delimiter = "$"
res = reduce ( lambda x, y: str (x) + delimiter + str (y), test_list)
print ( "The joined string : " + res)
|
Output
The Original List is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$9
Time Complexity: O(n), where n is the length of the input list
This is because in this case, the lambda function is applied to the entire list in a sequential manner.
Space Complexity: O(n), where n is the length of the input list
This is because the reduce() function requires additional memory to store intermediate results.
Method #5: Using str.rstrip()
This program is designed to remove the last occurrence of a delimiter from a list of integers and join them using the delimiter.
- Initialize the list.
- Print the original list.
- Initialize the delimiter.
- Join the list elements using the delimiter.
- The map() function is used to convert each element of the list to a string, and then join() is used to concatenate the elements with the delimiter.
- Remove the last occurrence of the delimiter using rstrip() method.
- The rstrip() method is used to remove the last occurrence of the delimiter from the end of the string.
- Print the result.
Python3
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The original list is : " + str (test_list))
delim = "$"
res = delim.join( map ( str , test_list))
res = res.rstrip(delim)
print ( "The joined string : " + str (res))
|
Output
The original list is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$9
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list, for storing the joined string.
Method 5: Using a loop and string concatenation
Step-by-step approach:
- Initialize the list test_list.
- Initialize the delimiter delim.
- Initialize an empty string res.
- Iterate over the elements of test_list except the last one, and concatenate each element with the delimiter and add it to res.
- Append the last element of test_list to res.
- Print the resulting string.
Below is the implementation of the above approach:
Python3
test_list = [ 4 , 7 , 8 , 3 , 2 , 1 , 9 ]
print ( "The original list is : " + str (test_list))
delim = "$"
res = ""
for i in range ( len (test_list) - 1 ):
res + = str (test_list[i]) + delim
res + = str (test_list[ - 1 ])
print ( "The joined string : " + str (res))
|
Output
The original list is : [4, 7, 8, 3, 2, 1, 9]
The joined string : 4$7$8$3$2$1$9
Time complexity: O(n)
Auxiliary space: O(n)
Please Login to comment...