Python: putting lists from a file into a list
Question: i'm very begginer in python.

i have a file with lists of coordinates. it seems like that :

    
    
    [-122.661927,45.551161], [-98.51377733,29.655474], [-84.38042879, 33.83919567].
    

i'm trying to put this into a list with:

    
    
    with open('file.txt', 'r') as f:
        for line in f:
            list.append(line)
    

the result i got is

    
    
    ['[-122.661927,45.551161], [-98.51377733,29.655474], [-84.38042879, 33.83919567]']
    

could sombody help me how can i get rid of the "'" marks at the beggining and
the end of the list?


Answer: Try using `ast.literal_eval`.

Example -

    
    
    import ast
    lst = []
    with open('file.txt', 'r') as f:
        for line in f:
            lst.extend(ast.literal_eval(line))
    

From documentation -

> **ast.literal_eval(node_or_string)**
>
> Safely evaluate an expression node or a Unicode or Latin-1 encoded string
> containing a Python literal or container display. The string or node
> provided may only consist of the following Python literal structures:
> strings, numbers, tuples, lists, dicts, booleans, and None.

Also, please note its bad to use `list` as a variable name, as it shadows the
`list` built-in function.

