Open In App

Python – K difference index pairing in list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# K difference index 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))
 
# initialize K
K = 3
 
# K difference index pairing in list
# using list comprehension + zip()
res = [i + j for i, j in zip(test_list, test_list[K :])]
 
# printing result
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




# Python3 code to demonstrate working of
# K difference index 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))
 
# initialize K
K = 3
 
# K difference index pairing in list
# using map() + concat
res = list(map(operator.concat, test_list[:-1], test_list[K:]))
 
# printing result
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




# Python3 code to demonstrate working of
# K difference index pairing in list
# using zip() + join()
   
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
   
# printing original list
print("The original list : " + str(test_list))
   
# initialize K
K = 3
   
# K difference index pairing in list
# using zip() + join()
res = list(map("".join, zip(test_list, test_list[K:])))
   
# printing result
print("List after K difference 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 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)
 
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# K difference index pairing in list
res = K_difference_pairing(test_list, K)
 
# printing result
print("List after K difference concatenation is : " + str(res))
#this code contributed by tvsk


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:

  1. Initialize an empty list to store the result.
  2. Use a for loop to iterate over the indices of the list from 0 to len(test_list) – K.
  3. For each index i, append the concatenation of test_list[i] and test_list[i+K] to the result list.
  4. Return the result list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# K difference index pairing in list
# using a loop
 
# initialize list
test_list = ["G", "F", "G", "I", "S", "B", "E", "S", "T"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# K difference index pairing in list
# using a loop
res = []
for i in range(len(test_list) - K):
    res.append(test_list[i] + test_list[i+K])
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads