Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Pair the consecutive character strings in a list

Python | Pair the consecutive character strings in a list

Sometimes while programming, we can face a problem in which we need to perform consecutive element concatenation. This problem can occur at times of school programming or competitive programming. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using list comprehension + zip() Combination of above functionalities can be used to solve this problem. In this, we iterate the list using list comprehension and formation of pairs using zip(). 

Python3




# Python3 code to demonstrate working of
# Consecutive element pairing in list
# using list comprehension + zip()
 
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
 
# printing original list
print("The original list : " + str(test_list))
 
# Consecutive element pairing in list
# using list comprehension + zip()
res = [i + j for i, j in zip(test_list, test_list[1:])]
 
# printing result
print("List after Consecutive concatenation is : " + str(res))


Output : 

The original list : [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’] List after Consecutive concatenation is : [‘GF’, ‘FG’, ‘GI’, ‘IS’, ‘SB’, ‘BE’, ‘ES’, ‘ST’]

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list comprehension + zip() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

  Method #2 : Using map() + concat() The combination of these functions can also perform this task. In this traversal logic is done by map() and concat performs the task of pairing. It’s more efficient than above method. 

Python3




# Python3 code to demonstrate working of
# Consecutive element pairing in list
# using map() + concat
import operator
 
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
 
# printing original list
print("The original list : " + str(test_list))
 
# Consecutive element pairing in list
# using map() + concat
res = list(map(operator.concat, test_list[:-1], test_list[1:]))
 
# printing result
print("List after Consecutive concatenation is : " + str(res))


Output : 

The original list : [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’] List after Consecutive concatenation is : [‘GF’, ‘FG’, ‘GI’, ‘IS’, ‘SB’, ‘BE’, ‘ES’, ‘ST’]

Method #3 : Using loop + range()

Another approach could be using a for loop and the range() function. We can iterate through the list using a for loop and use the range() function to get the index of the current element and the next element. Then we can concatenate the current element with the next element and append the result to a new list.

Python3




# Python3 code to demonstrate working of
# Consecutive element pairing in list
# using for loop and range()
   
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
   
# printing original list
print("The original list : " + str(test_list))
   
# Consecutive element pairing in list
# using for loop and range()
res = []
for i in range(len(test_list)-1):
    res.append(test_list[i] + test_list[i+1])
   
# printing result
print("List after Consecutive concatenation is : " + str(res))
#this code is contributed by edula vinay kumar reddy


Output

The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after Consecutive concatenation is : ['GF', 'FG', 'GI', 'IS', 'SB', 'BE', 'ES', 'ST']

This approach has a Time complexity of O(n) as we are iterating through the list once and space complexity of O(n) as we are creating a new list of the same size as the original list.

Method#4: Using NumPy module.

Algorithm:

  1. Import the numpy module
  2. Define a test list
  3. Use numpy’s char.add() function to concatenate consecutive elements of the list
  4. Convert the resulting numpy array back to a list
  5. Print the list

Python3




import numpy as np
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
   
# printing original list
print("The original list : " + str(test_list))
 
res = np.char.add(test_list[:-1], test_list[1:])
# printing result
print("List after Consecutive concatenation is : " + str(res.tolist()))
#this code contributed by tvsk.


Output:

The original list : [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’] List after Consecutive concatenation is : [‘GF’, ‘FG’, ‘GI’, ‘IS’, ‘SB’, ‘BE’, ‘ES’, ‘ST’]

Time complexity: O(n), where n is the length of the input list. This is because the numpy’s char.add() function iterates over the list only once.

Auxiliary space: O(n), where n is the length of the input list. This is because the numpy’s char.add() function creates a new array of size n-1 to store the concatenated elements.

Method#5:Using zip and slicing

Algorithm 

  1. Initialize the input list.
  2. Zip the original list with itself by slicing it, with second copy of list starting from second element to the end.
  3. Map each zipped tuple to the concatenated string obtained from the elements of tuple.
  4. Convert the map object to list.
  5. Print the list of consecutive element pairing in the list

Python3




# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
   
# printing original list
print("The original list : " + str(test_list))
 
# Consecutive element pairing in list
# using zip and slicing
res = [x+y for x,y in zip(test_list, test_list[1:])]
 
# printing result
print("List after Consecutive concatenation is : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after Consecutive concatenation is : ['GF', 'FG', 'GI', 'IS', 'SB', 'BE', 'ES', 'ST']

Time Complexity:
The time complexity of this algorithm is O(n) as the list needs to be iterated only once and zip, slicing and concatenation are constant time operations.

Auxiliary Space:
The space complexity of this algorithm is O(n) as it creates a new list of tuples equal to the size of the original list. However, the list of tuples is not stored in memory and only the final list is stored, which is equal to the size of the original list minus one.

RELATED ARTICLES

Most Popular

Recent Comments