- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find the character position of Kth word from a list of strings
When it is required to find the character position of ‘K’th word from a list of strings, a list comprehension along with enumerate is used.
Below is a demonstration of the same −
my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [element for sub in enumerate(my_list) for element in enumerate(sub)] my_result = my_result[K] print("The result is :") print(my_result)
The list is : ['python', 'is', 'fun', 'to', 'learn'] The value of K is : 15 The result is : 2
A list of strings is defined and is displayed on the console.
A value for ‘K’ is defined and is displayed on the console.
A list comprehension is used to iterate over the elements of the list using enumerate.
The zeroth element in every element is accessed using enumerate
This is converted to a list.
Since enumerate is used, the output would be an integer here.
The ‘K’th element of this list is displayed as the output on the console.
- Program to find the kth missing number from a list of elements in Python
- Python program to find word score from list of words
- Program to find the largest grouping of anagrams from a word list in Python
- Python – Test for Word construction from character list
- Program to find longest common prefix from list of strings in Python
- Program to find out if the strings supplied differ by a character in the same position in Python
- Program to find largest kth index value of one list in Python
- Python program for most frequent word in Strings List
- Python program – All occurrences of Substring from the list of strings
- Program to reverse the position of each word of a given string in Python
- Python Program to return the Length of the Longest Word from the List of Words
- Program to find the kth character after decrypting a string in C++
- Find frequency of given character at every position in list of lists in Python
- Program to find the kth factor of n using Python
- How to remove empty strings from a list of strings in Python?