Python | Merge list elements
Last Updated :
02 May, 2023
Sometimes, we require to merge some of the elements as single element in the list. This is usually with the cases with character to string conversion. This type of task is usually required in the development domain to merge the names into one element. Let’s discuss certain ways in which this can be performed.
Method #1 : Using join() + List Slicing The join function can be coupled with list slicing which can perform the task of joining each character in a range picked by the list slicing functionality.
Python3
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
print ("The original list is : " + str (test_list))
test_list[ 5 : 8 ] = [''.join(test_list[ 5 : 8 ])]
print ("The list after merging elements : " + str (test_list))
|
Output:
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using reduce() + lambda + list slicing The task of joining each element in a range is performed by reduce function and lambda. reduce function performs the task for each element in the range which is defined by the lambda function. It works with Python2 only
Python
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
print ("The original list is : " + str (test_list))
test_list[ 5 : 8 ] = [ reduce ( lambda i, j: i + j, test_list[ 5 : 8 ])]
print ("The list after merging elements : " + str (test_list))
|
Output:
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
Method #3 : Using + operator and del
This approach uses the += operator to concatenate the elements in the list, and the del statement to remove the merged elements.
Python3
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
test_list[ 5 ] + = ''.join(test_list[ 6 : 8 ])
del test_list[ 6 : 8 ]
print (test_list)
|
Output
['I', 'L', 'O', 'V', 'E', 'GFG']
Time complexity: O(n) ( To join all elements in worst case )
Auxiliary space: O(n) , since we are creating a new string to hold the concatenated elements.
Using the Map and Lambda Functions:
Another way to merge the elements of a list is to use the map function in combination with a lambda function. The map function applies the lambda function to each element in the list, and the lambda function concatenates the elements using the + operator.
Python3
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
print ( "The original list is : " + str (test_list))
result = ''.join( map ( lambda x: x, test_list[ 5 : 8 ]))
test_list[ 5 : 8 ] = [result]
print ( "The list after merging elements : " + str (test_list))
|
Output
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
This approach has a time complexity of O(n) and an auxiliary space of O(n), where n is the number of elements in the list.
Method #5 : Using a for loop
Python3
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
print ( "The original list is : " + str (test_list))
merged = ""
for i in range ( 5 , 8 ):
merged + = test_list[i]
test_list[ 5 : 8 ] = [merged]
print ( "The list after merging elements : " + str (test_list))
|
Output
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
The list after merging elements : ['I', 'L', 'O', 'V', 'E', 'GFG']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using heapq:
- Initialize the input list test_list.
- Create an empty string merged to store the merged elements.
- Use a for loop to iterate over the elements from indices 5 to 7 (exclusive) of test_list and concatenate them into the merged string.
- Replace the elements from indices 5 to 7 with the merged string using slicing.
- Print the updated list.
Python3
import heapq
test_list = [ 'I' , 'L' , 'O' , 'V' , 'E' , 'G' , 'F' , 'G' ]
print ( "The original list is:" , test_list)
merged = "".join(heapq.nlargest( 3 , test_list[ 5 : 8 ]))
test_list[ 5 : 8 ] = [merged]
print ( "The list after merging elements:" , test_list)
|
Output
The original list is: ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
The list after merging elements: ['I', 'L', 'O', 'V', 'E', 'GGF']
Time Complexity: O(n), where n is the length of the input list test_list. This is because the for loop iterates over the elements from indices 5 to 7 (exclusive), which takes O(1) time, and the slicing operation takes constant time.
Auxiliary Space: O(n), where n is the length of the input list test_list. This is because we are creating a string to store the merged elements, which takes O(n) space.
Please Login to comment...