Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Lowercase Kth Character in string
Next article icon

Python | Kth index character similar Strings

Last Updated : 28 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 combination of above two functions, list comprehension performs the task of extending the logic to whole list and lower function checks for case insensitivity with the target word of argument letter.

Python3




# Python3 code to demonstrate
# Kth index character similar Strings
# using list comprehension + lower()
 
# Initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# Initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# Printing original list
print("The original list : " + str(test_list))
 
# Kth index character similar Strings
# using list comprehension + lower()
res = [idx for idx in test_list if idx[K - 1].lower() == check.lower()]
 
# Printing the result
print("The list of matching Kth letter : " + str(res))
 
 
Output : 
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching Kth letter : ['Akash', 'akshat']

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #2: Using a for loop

  • Initialize an empty list res to store the matching strings.
  • Loop through each string in the test_list.
    • Use the lower() method to convert the current string to lowercase.
    • Use an if statement to check if the K-th character of the current string (also converted to lowercase) matches the check character (also converted to lowercase).
    • If the condition is true, append the current string to the res list.
  • Print the res list.

Python3




# Python3 code to demonstrate
# Kth index character similar Strings
# using for loop
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# printing original list
print("The original list : " + str(test_list))
 
# using for loop
# Kth index character similar Strings
res = []
for string in test_list:
    string_lower = string.lower()
    if string_lower[K-1] == check.lower():
        res.append(string)
 
# print result
print("The list of matching Kth letter : " + str(res))
 
 
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching Kth letter : ['Akash', 'akshat']

Time complexity: O(n*m), where n is the length of test_list and m is the length of the longest string in test_list. The for loop runs once for each string in test_list, and the lower() method has a time complexity of O(m)
Auxiliary space: O(k), where k is the number of strings in test_list that match the condition. 

Method 3:  using filter() function and lambda function:

Python3




# Python3 code to demonstrate
# Kth index character similar Strings
# using filter() + lambda function
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# printing original list
print("The original list : " + str(test_list))
 
# using filter() + lambda function
# Kth index character similar Strings
res = list(filter(lambda x: x[K-1].lower() == check.lower(), test_list))
 
# print result
print("The list of matching Kth letter : " + str(res))
 
 
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching Kth letter : ['Akash', 'akshat']

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(k), where k is the number of elements that satisfy the condition.



Next Article
Python - Lowercase Kth Character in string
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Similar characters Strings comparison
    Given two Strings, separated by delim, check if both contain same characters. Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!' Output : True Explanation : Same characters, just diff. positions. Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!' Output : False Ex
    6 min read
  • Python | K Character Split String
    The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Let’s discuss certain
    4 min read
  • Python | Strings with similar front and rear character
    Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using
    4 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 +
    4 min read
  • Python - Groups Strings on Kth character
    Sometimes, while working with Python Strings, we can have a problem in which we need to perform Grouping of Python Strings on the basis of its Kth character. This kind of problem can come in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1: Using loop Th
    4 min read
  • Python - Group Similar Start and End character words
    Sometimes, while working with Python data, we can have problem in which we need to group all the words on basis of front and end characters. This kind of application is common in domains in which we work with data like web development. Lets discuss certain ways in which this task can be performed. M
    5 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 perfor
    7 min read
  • Python - Test if Kth character is digit in String
    Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat
    5 min read
  • Similarity Metrics of Strings - Python
    In Python, we often need to measure the similarity between two strings. For example, consider the strings "geeks" and "geeky" —we might want to know how closely they match, whether for tasks like comparing user inputs or finding duplicate entries. Let's explore different methods to compute string si
    3 min read
  • Python - Characters Index occurrences in String
    Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using set() + reg
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences