Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - K difference index Pairing in List
K difference index pairing creates pairs of elements from a list where each element is paired with another element that is exactly K positions ahead. For example, with K=2, element at index 0 pairs with element at index 2, index 1 with index 3, and so on.
Given the list ["A", "B", "C", "D", "E", "F"] and K=2, the result would be ["AC", "BD", "CE", "DF"].
Using map() with operator.concat
The map() function applies operator.concat to corresponding elements from two sliced lists ?
import operator
# Initialize list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
print("The input list:", my_list)
# Initialize K value
K = 4
# K difference index pairing using map()
result = list(map(operator.concat, my_list[:-K], my_list[K:]))
print("List after K difference concatenation:", result)
The input list: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T'] List after K difference concatenation: ['TR', 'UI', 'TA', 'OL', 'RS', 'IP', 'AO', 'LI', 'SN', 'PT']
Using Recursion
A recursive approach processes the list by pairing the first element with the Kth element, then recursively processing the remaining elements ?
def k_diff_pairs(data_list, k):
# Base case: if k is greater than or equal to list length
if k >= len(data_list):
return []
# If only one pair possible
if len(data_list) == k + 1:
return [data_list[0] + data_list[k]]
# Recursive case: pair first with kth element, then process rest
return [data_list[0] + data_list[k]] + k_diff_pairs(data_list[1:], k)
# Initialize original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
print("The original input list:", my_list)
# Initialize K value
K = 3
# K difference index pairing using recursion
result = k_diff_pairs(my_list, K)
print("The K difference index pairing:", result)
The original input list: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T'] The K difference index pairing: ['TO', 'UR', 'TI', 'OA', 'RL', 'IS', 'AP', 'LO', 'SI', 'PN', 'OT']
Using While Loop
A simple iterative approach using a while loop to pair each element with the element K positions ahead ?
# Initialize original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
print("The input list:", my_list)
# Initialize K value
K = 2
# Using while loop for K difference index pairing
result = []
i = 0
while i < len(my_list) - K:
result.append(my_list[i] + my_list[i + K])
i += 1
print("The K difference index pairing:", result)
The input list: ['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T'] The K difference index pairing: ['TT', 'UO', 'TR', 'OI', 'RA', 'IL', 'AS', 'LP', 'SO', 'PI', 'ON', 'IT']
Using List Comprehension
The most concise approach using list comprehension to create pairs in a single line ?
# Initialize original list
my_list = ["A", "B", "C", "D", "E", "F", "G", "H"]
print("The input list:", my_list)
# Initialize K value
K = 3
# K difference index pairing using list comprehension
result = [my_list[i] + my_list[i + K] for i in range(len(my_list) - K)]
print("The K difference index pairing:", result)
The input list: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] The K difference index pairing: ['AD', 'BE', 'CF', 'DG', 'EH']
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| map() with operator | O(n) | O(n) | Moderate |
| Recursion | O(n) | O(n) | Complex |
| While Loop | O(n) | O(n) | Good |
| List Comprehension | O(n) | O(n) | Excellent |
Conclusion
K difference index pairing pairs elements that are K positions apart in a list. List comprehension provides the most readable and concise solution, while the map() approach offers a functional programming style.
