Skip to content
Related Articles

Related Articles

Python | Split a sentence into list of words

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 18 Aug, 2022
Improve Article
Save Article

Given a Sentence, write a Python program to convert the given sentence into a list of words. 

Examples: 

Input : 'Hello World'
Output : ['Hello', 'world']

Method 1: Split a sentence into a list using split()

The simplest approach provided by Python to convert the given list of Sentences into words with separate indices is to use split() method. This method split a string into a list where each word is a list item. We have alternative ways to use this function in order to achieve the required output.

Python3




# Driver code
lst =  "Geeks For geeks"
print( lst.split())

Output: 

['Geeks', 'For', 'geeks']

Method 2: Split a sentence into a list using for loop 

We can also use a Python for loop to split the first element. This method is also beneficial if we have more than one element.  

Python3




def convert(lst):
    return ([i for i in lst.split()])
     
# Driver code
lst =  'Geeksforgeeks is a portal for geeks'
print( convert(lst))

Output: 

['Geeksforgeeks', 'is', 'a', 'portal', 'for', 'geeks']

Method 3: Split a sentence into a list using join() 

We can split the given list and then join using join() function. We can also use this when you have a list of strings or a single string inside a list.  

Python3




def convert(lst):
    return ''.join(lst).split()
     
 
# Driver code
lst =  'Hello Geeks for geeks'
print( convert(lst))

Output: 

['Hello', 'Geeks', 'for', 'geeks']

Method 4: Split a sentence into a list using nltk

For our particular issue, the nltk library’s word tokenize() method can be used. This function divides a string into several substrings by taking a string as an input parameter.

Python3




import nltk
nltk.download('punkt')
 
string = "This is a sentence"
lst = nltk.word_tokenize(string)
print(lst)

Output:

['This', 'is', 'geeksforgeeks']

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!