Python | Pair Kth character with each element
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
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 4
res = []
for ele in test_str:
res.append(test_str[K] + ele)
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
from itertools import cycle
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 4
res = list ( map (''.join, zip (cycle(test_str[K]), test_str)))
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
- Initialize the given string “geeksforgeeks” to the variable test_str.
- Print the original string “geeksforgeeks”.
- 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.
- Use the map() function with a lambda function to iterate through each character in the string.
- 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.
- Store the result in the variable res as a list.
- Print the resulting list.
Python3
test_str = "geeksforgeeks"
print ( "The original string is : " + test_str)
K = 4
res = list ( map ( lambda x: test_str[K] + x, test_str))
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
Share your thoughts in the comments
Please Login to comment...