Posted by Marta on December 18, 2020 Viewed 4912 times
Convert a string to a list in python is a simple thing to do. It is quite a common thing that you will find yourself doing really often as a programmer. Some scenarios where converting a string to a list is useful:
There are different ways to convert a string to a list. I will introduce them in this article, along with some examples. The way you choose depends on the types of objects your final list will hold.
You might want to convert a string to a list of integers or to characters using python. Or converting to a list of strings, or a list of dictionaries. In this article I will show how you can achieve each of those cases, and provide some code challenges where this conversion is handy.
First scenario is converting a String to a list of String.
Input: "The sky is blue" Output: ["The", "sky", "is", "blue"]
You can achieve this using the .split()
method:
string_variable.split(delimiter)
Where delimiter is the character that will be used to chunk the string. If no delimiter is passed, the default delimiter will be white space. The split method will ignore any white spaces at the beginning or at the end of the text. Let’s see an example.
my_text = 'The sky is blue' my_text_with_extra_spaces = ' The sky is blue ' list_of_strings = my_text.split() list_of_strings_extra_spaces = my_text_with_extra_spaces.split() print(list_of_strings) print(list_of_strings_extra_spaces)
Output:
['The', 'sky', 'is', 'blue'] ['The', 'sky', 'is', 'blue']
As you can see, any extra spaces are ignored.
What if we want to split the strings to get the sentences within the string?You could do this just using “.” as the delimiter.
my_text = 'Sky is blue. Roses are red' my_text_with_extra_spaces = ' Sky is blue. Roses are red ' list_of_sentences = my_text.split('.') list_of_sentences_extra_spaces = my_text_with_extra_spaces.split('.') print(list_of_sentences) print(list_of_sentences_extra_spaces)
If you use any delimiter different of white space, the white spaces won’t be ignored.
Output:
['Sky is blue', ' Roses are red'] [' Sky is blue', ' Roses are red ']
What if you have several character that you would like to use as delimiters?Say we want to slip by any punctuation signs and white space.
Input: "The weather is great in this area, I know. Do you agree?.I don't. The weather is horrible here" Output :["The", "weather", "is","great","in","this","area",'"I","know","do", "you","agree","I", "don\'t","the","weather","is","horrible","here"]
There is a built-in way to do this using regular expressions. This method will chunk the string by the blocks that match the regular expression. Our delimiters are “.”(point), or “,”(comma), or “?”( question mark). Here is a regular expression that represents that :
'\\.|\s|\\,'
You will use the regular expression along with the split method from the re
(regular expression) python library:
import re re.split(regular_expression, text_to_split)
Putting those two together, the python code will look as follows:
import re my_text = 'The weather is great in this area, I know. Do you agree?.I don\'t. The weather is horrible here' list = re.split('\\.|\s|\\,', my_text) print(list)
Output:
['The', 'weather', 'is', 'great', 'in', 'this', 'area', '', 'I', 'know', '', 'Do', 'you', 'agree?', 'I', 'don\'t', '', 'The', 'weather', 'is', 'horrible', 'here']
What if you couldn’t use the split method? This may be the case if you are in an interview, where the interviewer want to test your logical thinking. A possible solution:
def convert_to_list(text): list = [] word = '' for character in text: #If we haven't found the delimiter, add character to the current word if(character!=' '): word=word+character else: # Found the delimiter, so the end of the word. Add the word to the list and clean the current word if len(word)>0 : list.append(word) word='' list.append(word) #Add last word to the list return list my_text = 'The sky is blue' list = convert_to_list(my_text) print(list)
What if at some point in the code, we want to convert a given string to a list of lists. Here is an example where this can be useful. Say that you have a bunch of word in a string, and you would like to know the number of sentences, and the number of words per sentence. This could be achieved easily by converting the string to a list of lists.
Let’s see how you can do this using python:
Input: 'Sky is blue. Roses are red' Output: [['Sky', 'is', 'blue'], ['Roses', 'are', 'red']]
my_text = 'Sky is blue. Roses are red' sentence_list = my_text.split('.') list_of_lists = [ sentence.split() for sentence in sentence_list ] print(list_of_lists)
Output:
[['Sky', 'is', 'blue'], ['Roses', 'are', 'red']]
First you will use the .split()
method to get all sentences in the string. Next you will loop through each sentence, split it by white space. This will return a list. And each list will be saved in the variable list_of_lists
Convert a string to character list. A possible case where this comes useful is if you are checking if a string is a palindrome, or any check of that sort.
Let’s see how to get a list of characters in python. This can be done is just one line.
Input: 'Sky is blue' Output: ['S', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e']
my_text = 'Sky is blue' characters = [character for character in my_text] print(characters) print(type(characters))
Output:
['S', 'k', 'y', ' ', 'i', 's', ' ', 'b', 'l', 'u', 'e'] <class 'list'>
What if you have some integers, or any numbers, and you would like to convert them to a list of integers. When could this be useful? Say that you want to extract some amounts from a text document, and calculate the average. You will receive the amounts as a string, convert them to numbers and calculate the average.
Let’s see two possible ways to do this in python
Input: '1,2,4,5,67' Output: [1, 2, 4, 5, 67]
AST stands for Abstract Syntax Tree, which is a built-in module that contains functionality to process python syntax grammar. One of the helper method this module provide is ast.literal_eval( expression)
. This method can evaluate is python expression saved in a string.
import ast my_text = '1,2,4,5,67' list_of_integers = ast.literal_eval('['+my_text+']') print(list_of_integers) print(type(list_of_integers))
Output:
[1, 2, 4, 5, 67] <class 'list'>
Another way to convert a string to a list of integers is using the json
module. Use this approach if the string follows the standard JSON notation.
json
is a built-in module, no extra installation steps to use it.
import json my_text = '1,2,4,5,67' list_of_integers_with_json = json.loads('['+my_text+']') print(list_of_integers_with_json) print(type(list_of_integers_with_json))
Output:
[1, 2, 4, 5, 67] <class 'list'>
Convert String to a Dictionary List. This can be handy if you store the some json on a database, for instance. You can convert the string back to a dictionary using the ast
module, or using the json
modules. Both approaches will give you the same end result.
Input: '{"id":1,"name":"Bob","age":20},{"id":2,"name":"Alice"}'
import ast my_text = '{"id":1,"name":"Bob","age":20},{"id":2,"name":"Alice"}' list_of_dictionaries = ast.literal_eval('[' + my_text + ']') print(list_of_dictionaries) print(type(list_of_dictionaries)) print(type(list_of_dictionaries[0]))
Output:
[{'id': 1, 'name': 'Bob', 'age': 20}, {'id': 2, 'name': 'Alice', 'age': 22}] <class 'list'> <class 'dict'>
import json my_text = '{"id":1,"name":"Bob","age":20},{"id":2,"name":"Alice"}' list_of_dictionaries = json.loads('['+my_text+']') print(list_of_dictionaries) print(type(list_of_dictionaries))
Output:
[{'id': 1, 'name': 'Bob', 'age': 20}, {'id': 2, 'name': 'Alice', 'age': 22}] <class 'list'> <class 'dict'>
What is you don’t add the square brackets? The string converts to a tuple.
You could also perform the opposite operation. List to String. This can be achieved easily using the str
method.
Input: ['Sky','is','really','blue'] Output: "['Sky', 'is', 'really', 'blue']"
list = ['Sky','is','really','blue'] list_string=str(list) print(list_string) print(type(list_string))
Output:
['Sky', 'is', 'really', 'blue'] <class 'str'>
Calculating the average number of words per sentence in a paragraph. My advice is you try to solve this yourself before checking the solution. 🙂
Input: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras suscipit nisl quam, eu auctor elit fermentum eu. Fusce faucibus ipsum erat, a semper ante pretium non. Proin blandit at diam eget tincidunt. Nunc efficitur nunc at nisi auctor malesuada. Phasellus a turpis non libero luctus semper. Donec vitae scelerisque tortor. Praesent vel nibh faucibus, aliquam nulla sit amet, aliquam augue. Vivamus vitae justo sodales, luctus erat ut, faucibus elit.
Solution
# Calculating the average number of words per sentence in a paragraph. input = """ any long text """ list_of_lists =[] sentences = input.split('.') list_of_lists = [ sentence.split() for sentence in sentences ] all_words_sum = sum([ len(list) for list in list_of_lists]) word_sentence_average = all_words_sum / len(list_of_lists) print(word_sentence_average)
In summary, there are a few ways to convert a string to list. Which way to choose depends of the type of information that the string is holding. Just text, or numbers or objects.
Hope you enjoy the article and find it useful. Thanks for reading and supporting this blog!
Happy coding! 🙂
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java