Python | Merge Range Characters in List
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 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 code to demonstrate # Merge Range Characters in List# using join() + list slicing # initializing list test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G'] # printing original listprint ("The original list is : " + str(test_list)) # initializing Range, i, ji, j = 3, 7 # using join() + list slicing# Merge Range Characters in Listtest_list[i : j] = [''.join(test_list[i : j])] # printing result print ("The list after merging elements : " + str(test_list)) |
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G'] The list after merging elements : ['I', 'L', 'O', 'VEGF', 'G']
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 code to demonstrate # Merge Range Characters in List# using reduce() + lambda + list slicing # initializing list test_list = ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G'] # printing original listprint ("The original list is : " + str(test_list)) # initializing strt, end strt, end = 3, 7 # using reduce() + lambda + list slicing# Merge Range Characters in Listtest_list[strt : end] = [reduce(lambda i, j: i + j, test_list[strt : end])] # printing result print ("The list after merging elements : " + str(test_list)) |
The original list is : ['I', 'L', 'O', 'V', 'E', 'G', 'F', 'G'] The list after merging elements : ['I', 'L', 'O', 'VEGF', 'G']


