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 - Eliminate Capital Letter Starting words from String
Next article icon

Extract words starting with K in String List – Python

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore various methods to extract words starting with K in String List. The simplest way to do is by using a loop.

Using a Loop

We use a loop (for loop) to iterate through each word in the list and check if it starts with the exact character (case-sensitive) provided in the variable.

Python
a = ["Kite", "Apple", "King", "Banana", "Kangaroo", "cat"]  # Character to match (case-sensitive) K = 'K'  # List to store words starting with given character res = []  # Loop through the list and check # if the word starts with character K for word in a:     if word.startswith(K):          res.append(word)  print(f"Words starting with '{K}':", res) 

Output
Words starting with 'K': ['Kite', 'King', 'Kangaroo'] 

Explanation:

  • word.startswith(K): Checks if word starts with the character stored in K without any case modification.
  • Only the words that exactly match the case of K will be included in the result.

Let’s explore other different methods:

Table of Content

  • Using List Comprehension
  • Using filter() Function

Using List Comprehension

List comprehension is a more concise and Pythonic approach to extract words starting with the specified character (case-sensitive).

Python
a = ["Kite", "Apple", "King", "Banana", "Kangaroo", "cat"]  # Character to match (case-sensitive) K = 'K'  # Using list comprehension to extract words # starting with the character K res = [word for word in a if word.startswith(K)]  print(f"Words starting with '{K}':", res) 

Output
Words starting with 'K': ['Kite', 'King', 'Kangaroo'] 

Using filter() Function

filter() function provides a functional programming approach to filter out words starting with the given character (case-sensitive).

Python
a = ["Kite", "Apple", "King", "Banana", "Kangaroo", "cat"]  # Character to match (case-sensitive) K = 'K'  # Using filter() and lambda function to extract # words starting with the character K res = list(filter(lambda word: word.startswith(K), a))  print(f"Words starting with '{K}':", res) 

Output
Words starting with 'K': ['Kite', 'King', 'Kangaroo'] 

Explanation:

  • filter() applies the lambda function to each word in the list a.
  • lambda word: word.startswith(K) checks if the word starts with the exact character stored in K.


Next Article
Python - Eliminate Capital Letter Starting words from String
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python | Extract Nth words in Strings List
    Sometimes, while working with Python Lists, we can have problems in which we need to perform the task of extracting Nth word of each string in List. This can have applications in the web-development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using list compreh
    7 min read
  • Extract List of Substrings in List of Strings in Python
    Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c
    3 min read
  • Python Program that Extract words starting with Vowel From A list
    Given a list with string elements, the following program extracts those elements which start with vowels(a, e, i, o, u). Input : test_list = ["all", "love", "get", "educated", "by", "gfg"] Output : ['all', 'educated'] Explanation : a, e are vowels, hence words extracted.Input : test_list = ["all", "
    5 min read
  • Python | Extract odd length words in String
    Sometimes, while working with Python, we can have a problem in which we need to extract certain length words from a string. This can be extraction of odd length words from the string. This can have application in many domains including day-day programming. Lets discuss certain ways in which this tas
    5 min read
  • Python - Eliminate Capital Letter Starting words from String
    Sometimes, while working with Python Strings, we can have a problem in which we need to remove all the words beginning with capital letters. Words that begin with capital letters are proper nouns and their occurrence mean different meaning to the sentence and can be sometimes undesired. Let's discus
    7 min read
  • Splitting String to List of Characters - Python
    We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python. Using list()The simplest way to split a string i
    2 min read
  • Splitting String to List of Characters - Python
    The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G
    3 min read
  • Python - Check if string starts with any element in list
    We need to check if a given string starts with any element from a list of substrings. Let's discuss different methods to solve this problem. Using startswith() with tuplestartswith() method in Python can accept a tuple of strings to check if the string starts with any of them. This is one of the mos
    3 min read
  • Python - Start and End Indices of words from list in String
    Given a String, our task is to write a Python program to extract the start and end index of all the elements of words of another list from a string. Input : test_str = "gfg is best for all CS geeks and engineering job seekers", check_list = ["geeks", "engineering", "best", "gfg"]Output : {'geeks': [
    7 min read
  • Finding Strings with Given Substring in List - Python
    The task of finding strings with a given substring in a list in Python involves checking whether a specific substring exists within any of the strings in a list. The goal is to efficiently determine if the desired substring is present in any of the elements of the list. For example, given a list a =
    3 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