Convert Sentence into List of Words & Vice Versa in Python (6 Examples)
Hello! This tutorial will show you 3 ways of converting a sentence into a list of words and 3 ways of converting that list of words back into a sentence in the Python programming language.
First, though, here is a quick overview of this tutorial:
Let’s get coding!
Create Sample Sentence
Here, we will create a simple sentence that we will use in all the examples in this tutorial. In your Python IDE, run the line of code below:
sentence = "the happy dog jumped over the bench"
Sentence to List | Example 1: split() Method
In this first example, we will make use of Python’s built-in split() method to split the sentence into a list of words:
word_list = sentence.split() print(word_list) # ['the', 'happy', 'dog', 'jumped', 'over', 'the', 'bench']
Sentence to List | Example 2: shlex.split() Function
In this second example, we will need to import the Python shlex module, which is a simple module for lexical analysis, in order to use its split() function to divide the sentence into a list of words:
import shlex word_list = shlex.split(sentence) print(word_list) # ['the', 'happy', 'dog', 'jumped', 'over', 'the', 'bench']
Sentence to List | Example 3: re.findall() Function
In this third and final example, we will import the Python regular expression module, whose findall() function will extract a list of words from the sentence:
import re word_list = re.findall(r"\b\w+\b", sentence) print(word_list) # ['the', 'happy', 'dog', 'jumped', 'over', 'the', 'bench']
\b\w+\b
is the regular expression for word search.
Here, we have examined 3 ways of converting a sentence into a list of words in the Python programming language. Next, we will examine 3 ways of concatenating the list of words to form a sentence.
List to Sentence | Example 1: join() Method
In this first example, we will use Python’s built-in join() method to combine the list of words to form a sentence:
sentence = " ".join(word_list) print(sentence) # the happy dog jumped over the bench
The join()
method concatenates the words as the opposite of what the split()
function does.
List to Sentence | Example 2: map() Function
In this second example, we will use Python’s built-in map() function, in combination with the join()
method, to convert the list of words back into a sentence:
sentence = " ".join(map(str, word_list)) print(sentence) # the happy dog jumped over the bench
The map()
function iterates through the list of words while the join()
method concatenates them into a sentence.
List to Sentence | Example 3: List Comprehension
In this third and final example, we will use list comprehension, in combination with the join()
method, to form a sentence from the list of words:
sentence = " ".join([str(i) for i in word_list]) print(sentence) # the happy dog jumped over the bench
There you have it! In this tutorial, we have examined 3 methods of extracting a list of words from a sentence and forming a sentence back from that list of words in the Python programming language.
I hope you found it helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a sentence into a list of words and vice versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a sentence into a list of words and vice versa in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Check if String Exists in List in Python (3 Examples)
- Access Dictionary within List in Python (Example)
- Convert List from Character String to Integer Python (2 Examples)
- Sort List of datetime Objects in Python (Example)
- Learn Python Programming
This post has shown how to convert a sentence into a list of words and vice versa in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter