Open In App

Python | Pair Kth character with each element

Last Updated : 24 Apr, 2023
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.



Similar Reads

Python | Insert character after every character pair
Sometimes, we can have a problem in which we need to insert a specific character after a pair(second) characters using Python. This kind of problem can occur while working with data, that require insertion of commas or any other special character mainly in the Machine Learning domain. Let's discuss certain ways in which this problem can be solved.
6 min read
Python - Closest Pair to Kth index element in Tuple
Sometimes, while working with Python records, we can have a problem in which we need to find the tuple nearest to certain tuple, query on a particular index. This kind of problem can have applications in data domains such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [(3, 4), (78, 76), (2, 3
7 min read
Python Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Python | Slicing list from Kth element to last element
Python list slicing slices the list from start index till end - 1, specified as list elements. So its tricky when we require to also slice the last element of list. Trying to slice till list size + 1 gives an error. Let's discuss ways in which last element can be included during a list slice. Method #1 : Using None During list slicing, giving the d
6 min read
Python | Insert Nth element to Kth element in other list
Sometimes, while working with Python list, there can be a problem in which we need to perform the inter-list shifts of elements. Having a solution to this problem is always very useful. Let’s discuss the certain way in which this task can be performed. Method 1: Using pop() + insert() + index() This particular task can be performed using a combinat
9 min read
Python - Convert each list element to key-value pair
Given list of elements, convert each element to a key-value pair dictionary, dividing digits equally. Input : test_list = [2323, 82, 129388, 234, 95] Output : {23: 23, 8: 2, 129: 388, 2: 34, 9: 5} Explanation : Digits distributed equally to keys and values. Input : test_list = [2323, 82, 129388] Output : {23: 23, 8: 2, 129: 388} Explanation : Digit
2 min read
Python | Remove Kth character from strings list
Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be performed. Method #1: Using list comprehension + list sl
7 min read
Python | Split string on Kth Occurrence of Character
Many problems of the split have been dealt with in the past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of a character. Let's discuss certain ways in which this problem can be solved. Method #1 : Using split() + join() The split and join method can perform this particular task. In t
6 min read
Python - Lowercase Kth Character in string
The problem of lowercasing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of string to lowercase. Let’s discuss certain ways in which this can be performed. Method #1 : Using string slicing + lower() This task can easily be performed using th
4 min read
Python | Kth index character similar Strings
Sometimes, we require to get the words that have the Kth index with the specific letter. This kind of use case is quiet common in places of common programming projects or competitive programming. Let’s discuss certain shorthand to deal with this problem in Python. Method #1: Using list comprehension + lower() This problem can be solved using the co
3 min read
Practice Tags :