
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert a string representation of list into list in Python
As python handles various data types, we will come across a situation where a list will appear in form of a string. In this article we will see how to convert a string into a list.
With strip and split
We first apply the strip method to remove the square brackets and then apply the split function. The split function with a comma as its parameter create the list from the string.
Example
stringA = "[Mon, 2, Tue, 5,]" # Given string print("Given string", stringA) print(type(stringA)) # String to list res = stringA.strip('][').split(', ') # Result and its type print("final list", res) print(type(res))
Output
Running the above code gives us the following result −
Given string [Mon, 2, Tue, 5,] final list ['Mon', '2', 'Tue', '5,']
With json.loads
The json module can make a direct conversion from string to list. We just apply the function by passing the string as a parameter. We can only consider numerical elements here.
Example
import json stringA = "[21,42, 15]" # Given string print("Given string", stringA) print(type(stringA)) # String to list res = json.loads(stringA) # Result and its type print("final list", res) print(type(res))
Output
Running the above code gives us the following result −
Given string [21,42, 15] final list [21, 42, 15]
With ast.literal_eval
The ast module gives us literal_eval which can directly convert the string into a list. We just supply the string as a parameter to the literal_eval method. We can only consider numerical elements here.
Example
import ast stringA = "[21,42, 15]" # Given string print("Given string", stringA) print(type(stringA)) # String to list res = ast.literal_eval(stringA) # Result and its type print("final list", res) print(type(res))
Output
Running the above code gives us the following result −
Given string [21,42, 15] final list [21, 42, 15]
- Related Articles
- Convert list of string into sorted list of integer in Python
- Convert list into list of lists in Python
- Convert list of tuples into list in Python
- Convert list of string to list of list in Python
- Python - Convert list of string to list of list
- Convert a nested list into a flat list in Python
- How can we convert a list of characters into a string in Python?
- Python - Convert given list into nested list
- Convert set into a list in Python
- Convert string enclosed list to list in Python
- Convert a list into tuple of lists in Python
- Convert list of numerical string to list of Integers in Python
- Convert list of tuples into digits in Python
- Convert String into comma separated List in Java
- Python Program To Convert An Array List Into A String And Viceversa
