• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 26, 2022 |4.6K Views

Python program to Convert a String representation of List into List

Description
Discussion

In this video, we will write a python program to convert a string representation of List into List

We will be using 2 approaches to do this:
1. Using split and strip
2. Using ast.literal_eval

Method 1: Using split and strip: In this method, first we strip '][' from a string representation of a list.
From the string after replacement, we split on ', ', so we get the required list of elements.

Method 2: Using ast.literal_eval: In this method, we pass a string representation of a list to literal_eval method of ast module. It returns our required Python list of elements.

Python program to convert a string representation of List into List
https://www.geeksforgeeks.org/python-program-to-print-even-numbers-in-a-list/