dantdj - 6 months ago 42

Python Question

I have a list containing the following, which is a bounding box formed of latitude and longitude points:

`[u'51.2867602', u'51.6918741', u'-0.510375', u'0.3340155']`

I'm currently attempting to convert each value inside this list to a float, in order to pass it into a different function. My current code is set up like this:

`for coordinate in coordinates:`

coordinate = float(coordinate)

However, this does not seem to work, as printing the list again provides the exact same output as the initial list. No error arises while running that code, however.

Any help on this would be much appreciated; my experience with Unicode is limited! I'm using Python 2.7.10.

Answer

`coordinate`

, in this context, is a *local variable*

```
for coordinate in coordinates:
coordinate = float(coordinate)
```

Therefore, assigning `float(coordinate)`

to the local variable will have no effect on the real list.

Instead, reference the list itself, like this:

```
for i in range(len(coordinates)):
coordinates[i] = float(coordinate)
```