Python program to extract keywords from the list

Here, we are going to learn how to extract all keywords from a list consisting of sentences in Python?
Submitted by Shivang Yadav, on March 25, 2021

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

Keywords are the reserved words in any programming language and their meanings/tasks are predefined, we cannot change their meanings.

We have a list of sentences, and we need to find all the keywords present in the list and print them all.

Example:

List:
["include help is" , "great platform for programmers"]

Keywords:
["is" , "for"]

We have a list of sentences and we need to find all keywords present in it. We will loop through the list and check each letter if it is a keyword using the isKeyword() method from the keyword module. Then store these keywords in a list and print it.

Program to extract keyword from the list

# Python Program to extract keywords from the list...

# Import iskeyword method
from keyword import iskeyword 

# Initializing list of sentences...
listOfSentence = ["include help is", "great platform for programmers"]

# Printing original list
print("List of sentences = ", end = " ")
print(listOfSentence)

keywordList = [] 
# Iterating using loop and checking for Keywords...
for sentence in listOfSentence:
    for words in sentence.split():
    	if iskeyword(words):
		    keywordList.append(words)

# Print keyword list...
print("Keywords present in List : ", end = " " )
print(keywordList)

Output:

List of sentences =  ['include help is', 'great platform for programmers']
Keywords present in List :  ['is', 'for']

Python List Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.