How to Convert String to Python List

Python is well known as one of the more versatile programming languages. This is in large part due to how the language handles data type structures. Strings and lists are two perfect examples of Python’s flexibility.

Strings in Python programming are generally analogous to the same variable in other languages. Meanwhile, lists are quite similar to arrays. But both data type are treated very differently in Python than programmers familiar with other languages might expect. In particular, we’re able to manipulate and even convert between these objects fairly easily. We can begin with a quick and simple demonstration of how we might convert a string into a Python list.

ourString = "We start with a string."
str2lst = ourString.split()
print("The created list: ", str2lst)

Our Python convert string to list example begins by simply defining a string literal. We then use the split function on the newly created ourString variable. Split is an immensely useful given string method that can take a string, convert list items, and return it as a standard Python list. In fact, we’ll see that in the next part of the Python program when we print it to screen. This is accomplished in the final line of the script.

But what about more complex cases? Python code often works with json string types. This might seem like a difficult usage scenario. But thankfully, json tools are part of the standard Python library. The following Python code will show how to use those functions to convert a json string to a list.

import json jsonConvertedString = '[{"a":1, "b":2}, {"c":3, "d":4}]'
ourList = json.loads(jsonConvertedString)
print(ourList[0])

In this example we import Python’s json library to convert a created json string into a list. Finally, we print the first list comprehension to show that it’s behaving as a standard Python list. Of course there’s a large number of other twists on strings to consider. One of the more difficult aspect of working with strings is the sheer scope of the subject. Basically, all plain text is a given string. Likewise, we’ll usually read data from outside sources into a split string value. This is even true if we’re reading in data that originates from something like a floating point number. The Python programming language often takes in information as a string and then gives us the option to convert it into other formats. We can then use a join function, cut the strings on whitespace or look for delimiter values to use as indicators of a point where converting string elements might end. We have a wide variety of methods to convert strings into other formats once we’ve properly split the elements into a character list.

We can see one example of this with something known as a csv or comma separated value file. For the sake of clarity, we’ll simply create a string where words are separated by commas. But when working with comma separated values we’ll typically read it from a file that’s been exported from spreadsheet or database programs.

ourString = "We,start,with,a,string"
print("List with a standard split = ", ourString.split() )
str2lst = ourString.split(",")
print ("csv = ",str2lst)

In the first line we begin with a comma separated string. We have multiple words within that single Python string. And each of those words is separated from the others with a comma. In the next line we call the print function with two parameters. The first parameter simply informs us of what we’re about to see. Specifically, we’re about to see what happens when we use the standard split function on our initial comma separated string.

On the next line we use a split in a slightly different way than we have up until this point. Previously, we hadn’t passed any parameters when calling split. But this time we’re passing a string into the split function. Whitespace is the default value that split uses to determine when one individual element of the Python string object ends and another begins. But if we pass a string conversion to the split string when we call it then the function will use that value as the separator. In the script’s final line we print out the result of that conversion.

How to Convert String to Python List

Leave a Reply

Your email address will not be published.

Scroll to top