Python program to find the character position of Kth word from a list of strings
Last Updated :
28 Apr, 2023
Given a list of strings. The task is to find the index of the character position for the word, which lies at the Kth index in the list of strings.
Examples:
Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 21
Output : 0
Explanation : 21st index occurs in "geeks" and point to "g" which is 0th element of word.
Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 15
Output : 1
Explanation : 15th index occurs in "best" and point to "e" which is 1st element of word.
Method #1 : Using enumerate() + list comprehension
In this, we use nested enumerate() to check indices for words, and strings in the list, list comprehension is used to encapsulate logic in 1 liner.
Python3
# Python3 code to demonstrate working of
# Word Index for K position in Strings List
# Using enumerate() + list comprehension
# initializing list
test_list = ["geekforgeeks", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 20
# enumerate to get indices of all inner and outer list
res = [ele[0] for sub in enumerate(test_list) for ele in enumerate(sub[1])]
# getting index of word
res = res[K]
# printing result
print("Index of character at Kth position word : " + str(res))
OutputThe original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Index of character at Kth position word : 2
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using next() + zip() + count()
In this, we pair up the number of words with their counts using zip(), and accumulate till we don't reach Kth Index.
Python3
# Python3 code to demonstrate working of
# Word Index for K position in Strings List
# Using next() + zip() + count()
from itertools import count
# initializing list
test_list = ["geekforgeeks", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 20
# count() for getting count
# pairing using zip()
cnt = count()
res = next(j for sub in test_list for j, idx in zip(
range(len(sub)), cnt) if idx == K)
# printing result
print("Index of character at Kth position word : " + str(res))
OutputThe original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Index of character at Kth position word : 2
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method 3: Using nested loop
Python3
# Python3 code to demonstrate working of
# Word Index for K position in Strings List
# Using nested loop
# initializing list
test_list = ["geekforgeeks", "is", "best", "for", "geeks"]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 20
# initializing index counter
idx = 0
# iterating over each word in the list
for word in test_list:
# if the kth position is in the current word
if idx + len(word) > K:
# printing result
print("Index of character at Kth position word : " + str(K - idx))
break
# if the kth position is not in the current word
else:
idx += len(word)
# if the kth position is beyond the end of the list
else:
print("K is beyond the end of the list")
OutputThe original list is : ['geekforgeeks', 'is', 'best', 'for', 'geeks']
Index of character at Kth position word : 2
Similar Reads
Find position of a character in given string - Python Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of the character 'e' is at index1. Let's look at various metho
2 min read
Python Program to Extract Strings with at least given number of characters from other list Given a list containing only string elements, the task is to write a Python program to extract all the strings which have characters from another list given a number of times. Examples: Input : test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"], char_list = ['e', 't', 's', 'm', 'n'], K = 2
7 min read
Python program to Concatenate Kth index words of String Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using joi
4 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read
Python program to print k characters then skip k characters in a string Given a String, extract K characters alternatively. Input : test_str = 'geeksgeeksisbestforgeeks', K = 4 Output : geekksisforg Explanation : Every 4th alternate range is sliced. Input : test_str = 'geeksgeeksisbest', K = 4 Output : geekksis Explanation : Every 4th alternate range is sliced. Method #
5 min read