Open In App

Python | Merge list elements

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# merging list elements
# using join() + list slicing
 
# initializing list 
test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using join() + list slicing
# merging list elements
test_list[5 : 8] = [''.join(test_list[5 : 8])]
 
# printing 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']

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




# Python code to demonstrate
# merging list elements
# using reduce() + lambda + list slicing
 
# initializing list 
test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using reduce() + lambda + list slicing
# merging list elements
test_list[5 : 8] = [reduce(lambda i, j: i + j, test_list[5 : 8])]
 
# printing 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']

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




# Initializing list
test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
 
# Merging elements using the += operator
test_list[5] += ''.join(test_list[6:8])
 
# Removing merged elements
del test_list[6:8]
 
# Printing the merged list
print(test_list)
# This code is contributed by Edula Vinay Kumar Reddy


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




# Initializing list
test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Merging elements using map and lambda
result = ''.join(map(lambda x: x, test_list[5:8]))
 
# Replacing merged elements with the result
test_list[5:8] = [result]
 
# Printing the merged list
print("The list after merging elements : " + str(test_list))
#This code is contributed by Edula Vinay Kumar Reddy


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))
#This code is contributed by Jyothi pinjala.


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:

  1. Initialize the input list test_list.
  2. Create an empty string merged to store the merged elements.
  3. 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.
  4. Replace the elements from indices 5 to 7 with the merged string using slicing.
  5. Print the updated list.

Python3




import heapq
 
test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G']
print("The original list is:", test_list)
 
# Merge elements from indices 5 to 7
merged = "".join(heapq.nlargest(3, test_list[5:8]))
test_list[5:8] = [merged]
 
print("The list after merging elements:", test_list)
#This code is contributed by Vinay Pinjala.


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.



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads