- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
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 extract Keywords from a list
When it is required to extract keywords from a list, a simple iteration and the ‘iskeyword’ method is used.
Example
Below is a demonstration of the same −
import keyword my_list = ["python", 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_result = [] for element in my_list: for word in element.split(): if keyword.iskeyword(word): my_result.append(word) print("The result is :") print(my_result)
Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ['is']
Explanation
A list of strings is defined and is displayed on the console.
An empty list is defined.
The list is iterated over, and every element is split based on spaces.
The ‘iskeyword’ method is used to check if any of the elements in the list are a keyword in the language.
If yes, it is appended to the empty list.
This list is displayed on the console as the output.
- Related Articles
- Python Program to Extract Elements from a List in a Set
- Python program to extract characters in given range from a string list
- Python Program that extract words starting with Vowel From A list
- Extract digits from Tuple list Python
- Python program to extract only the numbers from a list which have some specific digits
- Python – Extract element from a list succeeded by K
- Python – Extract elements from Ranges in List
- Python program to extract ‘k’ bits from a given position?
- List of Keywords in Python Programming
- Python Program to Extract Strings with at least given number of characters from other list
- Extract numbers from list of strings in Python
- Python Program to extract email-id from URL text file
- Python program to mask a list using values from another list
- How to extract first value from a list in R?
- Python program to remove Duplicates elements from a List?

Advertisements