Strings and lists are the most used python objects. Sometimes, while manipulating strings, we need to convert a string to a list. In this article, we will discuss different ways to convert a string to a list in Python.
Convert String to List Using List() Function in Python
The list()
function is used to create a list from an existing iterable object. The list()
function takes an iterable object as its input argument. After execution, it returns a list containing all the elements of the iterable object.
To convert a string to a list in Python, we will pass the input string to the list()
function. After execution, it will return a list containing the characters of the input string. You can observe this in the following example.
myStr = "pythonforbeginners"
output_list = list(myStr)
print("The input string is:", myStr)
print("The output list is:", output_list)
Output:
The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In the above example, we have passed the string pythonforbeginners
to the list()
function. After execution, it has returned the list of characters in the string.
String to List Using append() Method in Python
The append()
method is used to append an element to a list. When invoked on a list, it takes an element as its input argument and appends the element to the list.
To convert a string to a list using the append()
method, we will use the following steps.
- First, we will create an empty list named
myList
to store the output list. - After this, we will iterate through the characters of the input string using a for loop.
- During iteration, we will append the current character to
myList
using theappend()
method.
After executing the for loop, we will get the output list in myList
. You can observe this in the following example.
myStr = "pythonforbeginners"
myList = []
for character in myStr:
myList.append(character)
print("The input string is:", myStr)
print("The output list is:", myList)
Output:
The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
Here, you can observe that we have obtained a list from the input string using a for loop and the append()
method.
String to List Using extend() Method in Python
The extend()
method is used to add several elements to a list at the same time. The extend()
method, when invoked on a list, takes an iterable object as its input argument. After execution, it appends all the elements of the iterable object to the list.
To convert a string to a list using the extend()
method, we will first create an empty list named myList
. After that, we will invoke the extend()
method on myList
with the input string as an input argument to the extend()
method.
After execution of the extend()
method, we will get the resultant output in myList
. You can observe this in the following example.
myStr = "pythonforbeginners"
myList = []
myList.extend(myStr)
print("The input string is:", myStr)
print("The output list is:", myList)
Output:
The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
In the above example, we have obtained the list of characters of the string using the extend() method.
Convert String to List Using List comprehension in Python
List comprehension is used to create a new list from existing iterable objects. The syntax for list comprehension is as follows.
newList=[expression for element in iterable]
To convert a string to a list in python, we will use the input string in the place of iterable
. In the place of expression
and element
, we will use the characters of the string.
After execution of the above statement, we will get the output list in newList
.
You can observe this in the following example.
myStr = "pythonforbeginners"
myList = [character for character in myStr]
print("The input string is:", myStr)
print("The output list is:", myList)
Output:
The input string is: pythonforbeginners
The output list is: ['p', 'y', 't', 'h', 'o', 'n', 'f', 'o', 'r', 'b', 'e', 'g', 'i', 'n', 'n', 'e', 'r', 's']
Conclusion
In this article, we have discussed four ways to convert a string to a list in Python. Out of all these methods, you can use the list()
function to create a new list from the input string.
If you want to add the characters of the string to an existing list, you can use the approach with the append()
method or the extend()
method. To know more about python programming, you can read this article on string concatenation in Python. You might also like this article on dictionary comprehension in Python.
Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.