for loops, indexing, taking the min value of a list
Question: I have two shapefiles: of lakes and of cities. I need to find the closest city
to each lake and add the name of the city into the lake shapefile. I have:

    
    
    for lake in lake_cursor:
        lake_geom = lake.Shape
        city_dist_list = [] #create a city dis list = a list of dist from one lake to each city
        for cityID in range(0, city_length-1):
            #obtaining x and y for both cities and lakes
            cityX = citylist_X_Coor[cityID]
            cityY = citylist_Y_Coor[cityID]
            lakeX = lake_geom.centroid.X
            lakeY = lake_geom.centroid.Y
            #calculate distance
            dist = math.sqrt((cityX-lakeX)**2 + (cityY-lakeY)**2)
            #add the dist to the city dist list
            city_dist_list.append(dist)
        closest = min(city_dist_list)
        closestID = city_dist_list.index(closest)
    
        lake.City_Name = citylist_City_Name[closestID]
        lake.X_Coor = citylist_X_Coor[closestID]
        lake.Y_Coor = citylist_Y_Coor[closestID]
        print closest
    

but I keep getting an error message starting at `lake.City_Name`. The python
shell isn't telling me what is wrong - any ideas? And how to fix it as well?
Thanks!

the traceback shows:

    
    
    Traceback (most recent call last):
      File "C:\Users\xxx\xxx\xxx.py", line 71, in <module>
        lake.City_Name = citylist_City_Name[closestID]
      File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
        return setattr(self._arc_object, attr, ao)
    RuntimeError: ERROR 999999: Error executing function.
    


Answer: Without seeing the rest of your code, it's hard to tell for certain, but it
sure looks from the traceback like you are trying to do an update in a
SearchCursor. A SearchCursor's rows don't have setters. Make sure you are
using an UpdateCursor, and don't forget to do a `lake_cursor.updateRow(lake)`
for each lake you update. Below is a simple example of how to use an
UpdateCursor.

    
    
    import arcpy
    fc = 'c:/temp/temp.shp'
    rows = arcpy.UpdateCursor(fc)
    for row in rows:
        print row.Mapname
        row.Mapname = 'Some Value'
        rows.updateRow(row)
    
    del row, rows    
    

This example uses a regular `arcpy.UpdateCursor`. If you have ArcGIS 10.1,
then you can use an `arcpy.da.UpdateCursor`, which will run lot faster. Read
about these UpdateCursors
[here](http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000064000000)
and
[here](http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000014000000).

