-2

The problem is very fundamental. All I want to know is if there is a function to perform the task without appending to a new list.

I tried using loop but it will become a little complex for cases like... '[1,2,3],[4,5,6]'

  • 2
    `import ast; ast.literal_eval(your_string)`. Similar questions have been asked several times before (I am just too lazy to hunt for them now to mark as duplicate). – UltraInstinct Jul 04 '19 at 05:10
  • how did you get this string with list ? if it is JSON data then you can use `json` module. If you created it using `print(list)` or write(list) then maybe you should display/write it in different way or convert to something more useful. – furas Jul 04 '19 at 05:47
  • I am picking data from excel. And the data is a list of lists. – Hardik Chadda Jul 04 '19 at 05:59

2 Answers2

-1

The simple way:

import re

regex = re.compile("'|\[|\]")
new_list = re.sub(regex, "", '[1,2,3],[4,5,6]')

I'm sure that you can find a wiser way.

ozlevka
  • 1,550
  • 12
  • 23
-1
In [1]: a = '[1,2,3],[4,5,6]'                                                                                                                                           

In [2]: import ast                                                                                                                                                      

In [3]: ast.literal_eval(a)                                                                                                                                             
Out[3]: ([1, 2, 3], [4, 5, 6])

For ref : ast

Rohit-Pandey
  • 1,684
  • 14
  • 22