Open In App

Python | Pair Kth character with each element

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we need to perform the pairing of each character with every other in String. This can have application in many domains including web development and day-day. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop This task can be performed using loop. This a brute force manner in which this task can be performed. In this, we iterate each character and append the Kth letter to each and construct a list. 

Python3




# Python3 code to demonstrate working of
# Pair Kth character with each element
# Using loop
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 4
 
# Pair Kth character with each element
# Using loop
res = []
for ele in test_str:
        res.append(test_str[K] + ele)
 
# printing result
print("List after pairing : " + str(res))


Output : 

The original string is : geeksforgeeks
List after pairing : ['sg', 'se', 'se', 'sk', 'ss', 'sf', 'so', 'sr', 'sg', 'se', 'se', 'sk', 'ss']

  Method #2 : Using join() + zip() + cycle() The combination of above functions can be used to perform this task. In this, we perform the task of joining using join(). The task of pairing with all is done with zip() + cycle(). 

Python3




# Python3 code to demonstrate working of
# Pair Kth character with each element
# Using join() + zip() + cycle()
from itertools import cycle
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 4
 
# Pair Kth character with each element
# Using join() + zip() + cycle()
res = list(map(''.join, zip(cycle(test_str[K]), test_str)))
 
# printing result
print("List after pairing : " + str(res))


Output : 

The original string is : geeksforgeeks
List after pairing : ['sg', 'se', 'se', 'sk', 'ss', 'sf', 'so', 'sr', 'sg', 'se', 'se', 'sk', 'ss']

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(nlogn)

Auxiliary Space: O(n)

Method 3: Using List Comprehension
The task can be performed using the list comprehension and indexing.

Python3




test_str = "geeksforgeeks"
K = 4
 
result = [test_str[K] + ele for ele in test_str]
print("List after pairing:", result)


Output

List after pairing: ['sg', 'se', 'se', 'sk', 'ss', 'sf', 'so', 'sr', 'sg', 'se', 'se', 'sk', 'ss']

Time Complexity: O(n)
Space Complexity: O(n)
Explanation:
In this method, we are using list comprehension to loop through each element of the string and adding the Kth character to it, which is obtained by indexing the string. Finally, the result is stored in a list, which is then printed.

Method 4 : use the map function with lambda function. 

 step by step approach 

  1. Initialize the given string “geeksforgeeks” to the variable test_str.
  2. Print the original string “geeksforgeeks”.
  3. Initialize the variable K with the value 4. This will be used to select the Kth character of the string to pair with every other character in the string.
  4. Use the map() function with a lambda function to iterate through each character in the string.
  5. The lambda function takes a character as an argument, and returns a new string by concatenating the Kth character of the original string with the current character.
  6. Store the result in the variable res as a list.
  7. Print the resulting list.

Python3




# Python3 code to demonstrate working of
# Pair Kth character with each element
# Using map() and lambda function
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 4
 
# Pair Kth character with each element
# Using map() and lambda function
res = list(map(lambda x: test_str[K] + x, test_str))
 
# printing result
print("List after pairing : " + str(res))


Output

The original string is : geeksforgeeks
List after pairing : ['sg', 'se', 'se', 'sk', 'ss', 'sf', 'so', 'sr', 'sg', 'se', 'se', 'sk', 'ss']

Time complexity: O(n) where n is the length of the string.
Auxiliary space: O(n) as we are storing the result in a list.



Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads