Extract Keywords from a List of Strings - Python
Last Updated :
10 Feb, 2025
We are given a list of strings and our task is to extract all words that are valid Python keywords. [Python keywords are reserved words that define the language's syntax (e.g., is, True, global, try).] For example: a = ["Gfg is True", "Its a global win", "try Gfg"] then the output will be: ['is', 'True', 'global', 'try'].
Using a Set
In this method we iterate over each string and split it into words, for each word we use keyword.iskeyword() to check if it is a valid Python keyword then we use a set to keep track of the keywords we have already added so that each keyword appears only once preserving the order of first occurrence.
Python
import keyword
a = ["Gfg is True", "Its a global win", "try Gfg"]
seen = set() # To store keywords that have been added
res = [] # List to store unique keywords in order
for s in a:
for w in s.split():
if keyword.iskeyword(w) and w not in seen: # Check if w is a keyword and not already added
seen.add(w)
res.append(w)
print(res)
Output['is', 'True', 'global', 'try']
Explanation: Loop through each string and then each word in the string to check if the word is a Python keyword and ensure it hasn't been added before (using the set seen).
Using List Comprehension
In this method we first create a flattened list of words from all strings while filtering out only the valid Python keywords using keyword.iskeyword(). We then use dict.fromkeys() to remove duplicates while preserving the order of appearance.
Python
import keyword
a = ["Gfg is True", "Its a global win", "try Gfg"]
words = [w for s in a for w in s.split() if keyword.iskeyword(w)] # Extract keywords from each string
res = list(dict.fromkeys(words)) # Remove duplicates while preserving order
print(res)
Output['is', 'True', 'global', 'try']
Explanation:
- List comprehension flattens the list of strings into individual words and filters for keywords.
- dict.fromkeys(words) removes duplicate keywords while keeping the original order (since dictionaries preserve insertion order in Python 3.7+).
Using filter with a Lambda Function
This method is similar to Method 2 but instead of a list comprehension we use filter() with keyword.iskeyword to extract the keywords from the flattened list and then remove duplicates using dict.fromkeys().
Python
import keyword
a = ["Gfg is True", "Its a global win", "try Gfg"]
words = list(filter(keyword.iskeyword, [w for s in a for w in s.split()])) # Filter keywords
res = list(dict.fromkeys(words)) # Remove duplicates while preserving order
print(res)
Output['is', 'True', 'global', 'try']
Explanation:
- Inner list comprehension gathers all words and filter(keyword.iskeyword, ...) retains only the Python keywords.
- We remove duplicates and preserve order with dict.fromkeys().
Using Nested Loops
In this method we again iterate over each string and word, check if the word is a Python keyword and add it to the result list only if it is not already present. However, using if w not in res for duplicate checking is less efficient for larger datasets due to O(n) membership tests on a list.
Python
import keyword
a = ["Gfg is True", "Its a global win", "try Gfg"]
res = [] # List to store unique keywords
for s in a:
for w in s.split():
if keyword.iskeyword(w) and w not in res: # Check membership in list (less efficient)
res.append(w)
print(res)
Output['is', 'True', 'global', 'try']
Explanation:
- We use nested loops to process each string and each word within it.
- The condition w not in res ensures that duplicates are not added but this check is O(n) per lookup, making it less efficient for larger input sizes.
Similar Reads
Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read
Extract Substrings From A List Into A List In Python Python is renowned for its simplicity and versatility, making it a popular choice for various programming tasks. When working with lists, one common requirement is to extract substrings from the elements of the list and organize them into a new list. In this article, we will see how we can extract s
2 min read
Python - Group list by first character of string Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These types of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by
7 min read
Python | Extract words from given string In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
4 min read
Python - Extract K length substrings The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in PythonUsing List Comprehension List comprehension is t
2 min read
Python - Extracting Key from Value Substring Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the key from given value, querying substring from key's value. This kind of problem is common and have application in many domains including web development. Lets discuss certain ways in which this task
5 min read
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
Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli
2 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
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 in
2 min read