Python - K difference index Pairing in List


The K is the special number that set the sum of the difference values starting with 0th index. For example, if k = 3 it means the 0th index added with K and finds the exact pairs. In Python, we have some built-in functions such as str(), len(), and append() will be used to solve the K difference index pairing in list.

Let’s take an example of list.

The given list, [“A”, “B”, “C”, “D”, “E”, “F”]

Then the K value set to 1

The final output pair the index as [“AC”, “BD”, “CE”, “DF”]

Syntax

The following syntax is used in the examples-

str()

The built-in function str() converts the value into a string so it can be integrated with other strings.

len()

The len() is an in-built function in Python that returns the length of the objects.

append()

The built-in function append() add the element at the end of the list.

Using map() function

In the following example, start the program with the operator library that provides various built-in functions related to operators. Then create the original list my_list to store the string and display the list. Next, initialize the initial value of K that will be used to set the difference index pairing in the list. Then use the built-in function map() that accepts a few parameters operator.concat(used for index pairing), and slicing notation(my_list[:-1], my_list[K:]) to pair with specific index according to K value. Finally, display the result.

Example

import operator
# initialize list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# print the original list
print("The input list : " + str(my_list))
# Initiaze the initial value of K
K = 4
# The K difference index pair using map()
result = list(map(operator.concat, my_list[:-1], my_list[K:]))
# printing result
print("List after K difference concatenation is : " + str(result))

Output

The input list
['T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S', 'P', 'O', 'I', 'N', 'T'] 
List after K difference concatenation is : ['TR', 'UI', 'TA', 'OL', 'RS', 'IP', 'AO', 'LI', 'SN', 'PT']

Using recursion

In the following example, the program uses the recursive function f_diff_pairs() that accepts two parameters l and k to fetch the value from the original list variable my_list and K value respectively. Then use the first if statement that set the condition as the K value less than equal to the length of the original list and returns an empty list. Then using another if-statement it set the condition that if the value of K is equivalent to 1. It returns the list comprehension to calculate the K difference starting from index 1 otherwise it will return the indexing starting from the 0th index of original list and calculate the index pairing in list.

Example

def K_diff_pairs(l, K):
	if K >= len(l):
		return []
	if K == 1:
		return [l[i]+_list[i+1] for i in range(len(l)-1)]
	return [l[0]+l[K]] + K_diff_pairs(l[1:], K)
# Intialization of original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# printing original list
print("The original input list: " + str(my_list))
# initialize the initial value of K
K = 3
# K difference index pairing in list
res = K_diff_pairs(my_list, K)
# print the result
print("The K difference index pairing: " + str(res))

Output

 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 simple while loop

In the following example, start initializing the input list in the variable my_list and display the same. Then initialize the K value to 2 which means every index pairs with the next index follows the difference of 2. Initialize the empty list in the variable res that will be used to store the final result. Using a while loop, the variable i iterate through the length(my_list) - k of the original list. Then use the built-in function append() that inserts the element by calculating the sum of the original list index my_list[i] with k value i.e. my_list[i+k]. Then using += operator the variable iterates the particular index of an original list. Finally, it will print the result.

Example

# Initialization of original list
my_list = ["T", "U", "T", "O", "R", "I", "A", "L", "S", "P", "O", "I", "N", "T"]
# print the original list
print("The input list: " + str(my_list))
# Initialize the initial value of K
K = 2
# Using a while loop: K difference index pairing in list
res = []
# Initialize the initial value of i
i = 0
while i < len(my_list) - K:
    res.append(my_list[i] + my_list[i+K])
    i += 1
# print the final result
print("The K difference index pairing: " + str(res))

Output

 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']

Conclusion

The K difference is a specific value that pairs the two-element list by using some specific condition and operation. The program uses various built-in methods such as list comprehension, while loop, map(), and, list(). This type of program is generally used to build the connection of requirement data.

Updated on: 17-Aug-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements