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>As an alternative to using dictionaries, consider using namedtuples to store your position data. They use less RAM, and the components can be accessed either by index or as attributes. Since namedtuples are a type of tuple they are immutable, but it generally make sense for (latitude, longitude) info of a place to be stored in an immutable data structure. :) </p>

<p>Here's a short demo.</p>

<pre><code>from collections import namedtuple

Pos = namedtuple('Pos', ('lat', 'lng'))

a = Pos(30, 150)
print(a)
print(a[0], a.lng)

coords = [ 
    [4.54323, 5.4325],
    [7.235, 3.67543],
    [9.342543, 1.65323],
]

locations = [Pos(*u) for u in coords]
print(locations)
</code></pre>

<p><strong>output</strong></p>

<pre><code>Pos(lat=30, lng=150)
30 150
[Pos(lat=4.54323, lng=5.4325), Pos(lat=7.235, lng=3.67543), Pos(lat=9.342543, lng=1.65323)]
</code></pre>

python string list set jinja2