
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert string representation of list to list in Python?
We can use ast.literal_eval() here to evaluate the string as a python expression. It safely evaluates an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
example
fruits = "['apple', 'orange', 'banana']" import ast fruits = ast.literal_eval(fruits) print fruits[1], fruits[0]
Output
This will give us the output −
orange, apple
- Related Questions & Answers
- Convert a string representation of list into list in Python
- Python - Convert list of string to list of list
- Convert list of string to list of list in Python
- Convert string enclosed list to list in Python
- Convert list of numerical string to list of Integers in Python
- How to convert list to string in Python?
- Convert list of tuples to list of list in Python
- Convert list of string into sorted list of integer in Python
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Python program to convert a list to string
- How to convert a string to a list of words in python?
- Python - Convert List of lists to List of Sets
- Convert a list to string in Python program
- Convert list of strings to list of tuples in Python
Advertisements