Python – K difference index pairing in list
Sometimes while programming, we can face a problem in which we need to perform K difference 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
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
print ("The original list : " + str (test_list))
K = 3
res = [i + j for i, j in zip (test_list, test_list[K :])]
print (" List after K difference concatenation is : " + str (res))
|
Output :
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after K difference concatenation is : ['GI', 'FS', 'GB', 'IE', 'SS', 'BT']
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. list comprehension + zip() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the 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
import operator
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
print ("The original list : " + str (test_list))
K = 3
res = list ( map (operator.concat, test_list[: - 1 ], test_list[K:]))
print (" List after K difference concatenation is : " + str (res))
|
Output :
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after K difference concatenation is : ['GI', 'FS', 'GB', 'IE', 'SS', 'BT']
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using map() + concat() 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 #3 : Using join()
This is the most efficient method as it uses the combination of zip and join functions to perform this task.
Python3
test_list = [ "G" , "F" , "G" , "I" , "S" , "B" , "E" , "S" , "T" ]
print ( "The original list : " + str (test_list))
K = 3
res = list ( map ("".join, zip (test_list, test_list[K:])))
print ( "List after K difference concatenation is : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after K difference concatenation is : ['GI', 'FS', 'GB', 'IE', 'SS', 'BT']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using Recursive method.
Python3
def K_difference_pairing(test_list, K):
if K > = len (test_list):
return []
if K = = 1 :
return [test_list[i] + test_list[i + 1 ] for i in range ( len (test_list) - 1 )]
return [test_list[ 0 ] + test_list[K]] + K_difference_pairing(test_list[ 1 :], K)
test_list = [ "G" , "F" , "G" , "I" , "S" , "B" , "E" , "S" , "T" ]
print ( "The original list : " + str (test_list))
K = 3
res = K_difference_pairing(test_list, K)
print ( "List after K difference concatenation is : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after K difference concatenation is : ['GI', 'FS', 'GB', 'IE', 'SS', 'BT']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using a loop to iterate over the list
Step-by-step approach:
- Initialize an empty list to store the result.
- Use a for loop to iterate over the indices of the list from 0 to len(test_list) – K.
- For each index i, append the concatenation of test_list[i] and test_list[i+K] to the result list.
- Return the result list.
Below is the implementation of the above approach:
Python3
test_list = [ "G" , "F" , "G" , "I" , "S" , "B" , "E" , "S" , "T" ]
print ( "The original list : " + str (test_list))
K = 3
res = []
for i in range ( len (test_list) - K):
res.append(test_list[i] + test_list[i + K])
print ( "List after K difference concatenation is : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after K difference concatenation is : ['GI', 'FS', 'GB', 'IE', 'SS', 'BT']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...