How to Merge Elements in a Python Sequence?

Python sequences include strings, lists, tuples, and other iterable data structures. You can merge elements within a sequence using various methods like join(), reduce() with lambda functions, list comprehensions, and zip().

Using join() Method

The join() method concatenates string elements in a sequence

# List of characters
letters = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U']

# Display the original list
print("List =", letters)

# Merge first 3 elements using join()
letters[0:3] = [''.join(letters[0:3])]

# Display the result
print("Result =", letters)
List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U']
Result = ['HOW', 'A', 'R', 'E', 'Y', 'O', 'U']

Using reduce() with Lambda

The reduce() function from the functools module applies a lambda function cumulatively to merge elements

import functools

# List of characters
letters = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U']

# Display the original list
print("List =", letters)

# Merge first 3 elements using reduce and lambda
letters[0:3] = [functools.reduce(lambda i, j: i + j, letters[0:3])]

# Display the result
print("Result =", letters)
List = ['H', 'O', 'W', 'A', 'R', 'E', 'Y', 'O', 'U']
Result = ['HOW', 'A', 'R', 'E', 'Y', 'O', 'U']

Using List Comprehension

List comprehension provides a concise way to merge adjacent pairs of elements

# List of names and scores
student_data = ['john', '96', 'tom', '90', 'steve', '86', 'mark', '82']

# Display the original list
print("List =", student_data)

# Merge pairs of elements (name and score)
merged_data = [student_data[i] + " " + student_data[i+1] for i in range(0, len(student_data), 2)]

# Display the result
print("Result =", merged_data)
List = ['john', '96', 'tom', '90', 'steve', '86', 'mark', '82']
Result = ['john 96', 'tom 90', 'steve 86', 'mark 82']

Using zip() with Slicing

The zip() function pairs elements from even and odd indices, then joins them

# List of names and scores
student_data = ['john', '96', 'tom', '90', 'steve', '86', 'mark', '82']

# Display the original list
print("List =", student_data)

# Merge using zip() with slice notation
merged_data = [':'.join(item) for item in zip(student_data[::2], student_data[1::2])]

# Display the result
print("Result =", merged_data)
List = ['john', '96', 'tom', '90', 'steve', '86', 'mark', '82']
Result = ['john:96', 'tom:90', 'steve:86', 'mark:82']

Comparison of Methods

Method Use Case Flexibility
join() Simple string concatenation Limited to strings
reduce() Complex merging logic High flexibility
List comprehension Pairwise merging Readable and efficient
zip() Merging alternating elements Great for structured data

Conclusion

Use join() for simple string merging, list comprehension for readable pairwise operations, and zip() when working with structured alternating data. Choose reduce() for complex custom merging logic.

Updated on: 2026-03-26T21:31:09+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements