2

I have a list of tuples saved into a string (unfortunately). I am looking for a fast an efficient way to convert this string to an actual list of tuples.

Example:

mylist_string = '[(40.7822603, -73.9525339), (40.7142, -74.0087), (40.7250027, -73.9413106), (40.703422, -73.9862948), (40.7169963, -74.0149991), (40.7420448, -73.9918131), (40.7287, -73.9799), (40.7757237, -73.9492357), (40.7169904, -73.9578252), (40.726103, -73.9780367), (40.7776792, -73.9585829), (40.6750972, -73.9679734), (40.6867687, -73.9743078), (40.6684762, -73.9755826), (40.7169, -73.9578), (40.6996798, -73.9291393), (40.6680182, -73.9809183), (40.7346, -74.0073), (40.6871087, -73.9741862), (40.7160416, -73.9452393), (40.7178984, -74.0063829)]'

the expected output is a list of tuples in Python.

Thanks

strv7
  • 109
  • 10

2 Answers2

6

This is one way:

import ast

ast.literal_eval(mystr)

# [(40.7822603, -73.9525339),
#  (40.7142, -74.0087),
#  (40.7250027, -73.9413106),
#  (40.703422, -73.9862948),
# ...
#  (40.6871087, -73.9741862),
#  (40.7160416, -73.9452393),
#  (40.7178984, -74.0063829)]
jpp
  • 159,742
  • 34
  • 281
  • 339
  • What would be the benefit of using `ast` over the built-in `eval` function? – mypetlion Feb 10 '18 at 00:16
  • `ast.literal_eval` raises an exception if the input isn't a valid Python datatype. It is good practice. – jpp Feb 10 '18 at 00:16
  • `eval` is usually *never* the answer. Read [this](https://stackoverflow.com/questions/9383740/what-does-pythons-eval-do) and [this](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) for more information. – idjaw Feb 10 '18 at 00:20
  • 3
    @mypetlion difference is that you'll probably get fired from a job if you use `eval`. – RoadRunner Feb 10 '18 at 00:23
  • @mypetlion Concerning your question about using ast over eval, read [this](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval) – idjaw Feb 10 '18 at 00:27
-1
eval(mylist_string)

This will evaluate your string as if you typed it in Python.

Edit: this is an answer to the question, but it is not a good answer. There are security risks and it might not do what you think it does. So don't use eval, use:

import ast

ast.literal_eval(mystr)

See [this SO question] for a detailed comparison.

briancaffey
  • 2,339
  • 6
  • 34
  • 62
  • Can I note that this can be a security issue. If you have not sanitized your input, that is. – Mr.Zeus Feb 10 '18 at 00:16
  • @Mr.Zeus thanks, that's an interesting point. I just found this article titled [Eval is really dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). – briancaffey Feb 10 '18 at 00:17
  • 1
    For example: `exec(os.system("sh"))` will open a shell. So be careful if you are taking any input for users and/or editable file(files that can be edited by a user without admin/sudo permissions). – Mr.Zeus Feb 10 '18 at 00:19
  • `eval` is usually *never* the answer. Read [this](https://stackoverflow.com/questions/9383740/what-does-pythons-eval-do) and [this](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) for more information. – idjaw Feb 10 '18 at 00:20
  • I some ways I don't understand why the `eval` statement is still part of python. – Mr.Zeus Feb 10 '18 at 00:29
  • @Mr.Zeus This answer provides some information on that: https://stackoverflow.com/a/1087265/1832539 – idjaw Feb 10 '18 at 00:32