
- 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 list of string to list of list in Python
In this article we will see how to create a list of lists which contain string data types. The inner list themselves or of string data type and they may contain numeric or strings as their elements.
Using strip and split
We use these two methods which will first separate out the lists and then convert each element of the list to a string.
Example
list1 = [ '[0, 1, 2, 3]','["Mon", "Tue", "Wed", "Thu"]' ] print ("The given list is : \n" + str(list1)) print("\n") # using strip() + split() result = [k.strip("[]").split(", ") for k in list1] print ("Converting list of string to list of list : \n" + str(result))
Output
Running the above code gives us the following result −
The given list is : ['[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]'] Converting list of string to list of list : [['0', '1', '2', '3'], ['"Mon"', '"Tue"', '"Wed"', '"Thu"']]
Using slice and spilt
In this approach, we will use string slicing and then split the string to get the list of lists. Here the split function is applied along with a for loop.
Example
list1 = [ '[0, 1, 2, 3]','["Mon", "Tue", "Wed", "Thu"]' ] print ("The given list is : \n" + str(list1)) print("\n") # using split() result = [i[1 : -1].split(', ') for i in list1] print ("Converting list of string to list of list : \n" + str(result))
Output
Running the above code gives us the following result −
The given list is : ['[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]'] Converting list of string to list of list : [['0', '1', '2', '3'], ['"Mon"', '"Tue"', '"Wed"', '"Thu"']]
- Related Articles
- Python - Convert list of string to list of list
- Convert list of numerical string to list of Integers in Python
- Convert string enclosed list to list in Python
- Convert list of tuples to list of list in Python
- Convert a string representation of list into list in Python
- Convert list of string into sorted list of integer in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Python - Convert List of lists to List of Sets
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Convert list of tuples into list in Python
- Convert list into list of lists in Python
- How to convert list to string in Python?
- Convert list of strings and characters to list of characters in Python

Advertisements