Ignore or sidestep a name error
<p>I feel like I'm turning a molehill into a mountain here but I can't think of or don't know of a better way to do this. </p>

<p>In short, <strong>I have</strong> a list of lists that looks like: </p>

<p><code>[ [4.54323, 5.4325], [7.235, 3.67543], [9.342543, 1.65323] ]</code></p>

<p>And <strong>I need</strong> a list that looks like the following:</p>

<p><code>[ {lat: 4.54323, lng: 5.4325}, {lat: 7.235, lng: 3.67543}, {lat: 9.342543, lng: 1.65323} ]</code></p>

<p>I've done some things I'm not proud of and have ended up with:</p>

<p><code>[ '{lat: 4.54323, lng: 5.4325}', '{lat: 7.235, lng: 3.67543}', '{lat: 9.342543, lng: 1.65323}' ]</code></p>

<p>Yup, some string manipulation and now I'm <em>stuck</em> with a list of strings. I then tried to evaluate each element in said list but got a <code>NameError: name 'lat' is not defined.</code> unsurprisingly. </p>

<p>So this more of a multi-part question. Is there a way to get what I want without turning elements into strings and replacing characters? Or alternatively, can I evaluate or turn these strings into whatever data type they should be (I think sets). This variable is only going to be sent to a HTML file to replace a variable by using Jinja. So it doesn't really need to be understood by python, (I'm using 2.7.x).</p>

<p><strong>EDIT:</strong> This is the method I'm working with, I'm actively trying not to share that much code because of work reasons, sorry. There's also a lot of different attempts been made in the lifetime of the method. So some lines may look weird.</p>

<pre><code>evalledCoords = []

def make_points(coords):
if(coords):
    for x in range(0, len(coords)):
        # remove first element, not needed.
        coords[x].pop(0)
        coords[x] = str(coords[x]).replace("[", "{lat: ").replace(",", ", lng:").replace("]", "}")
        # See outputs
        print '\n', coords[x]
        print 'evalled coords', evalledCoords.append(eval(coords[x]))
return coords
</code></pre>

<p>Based on the provided data structures and what expected output you are looking to achieve, this can be done through a simple loop over your existing data structure and creating a new list of dictionaries:</p>

<pre><code>coords = [ [4.54323, 5.4325], [7.235, 3.67543], [9.342543, 1.65323] ]

new_list = []
for data in coords:
    new_list.append({
        'lat': data[0],
        'lng': data[1]
    })

print(new_list)
# [{'lng': 5.4325, 'lat': 4.54323}, {'lng': 3.67543, 'lat': 7.235}, {'lng': 1.65323, 'lat': 9.342543}]
</code></pre>

<p>Or, simply as a list comprehension:</p>

<pre><code>new_list = [{'lat': d[0], 'lng': d[1]} for d in coords]

print(new_list)
# [{'lng': 5.4325, 'lat': 4.54323}, {'lng': 3.67543, 'lat': 7.235}, {'lng': 1.65323, 'lat': 9.342543}]
</code></pre>

python string list set jinja2