I have two layers. The first is bicycle stations, the second is bicycle rentals on a specific day. I want to make a selection by station number attribute using the station list contained in the first layer and save in geodatabase.

What am I doing wrong?

arcpy.env.overwriteOutput = True


arcpy.MakeFeatureLayer_management(points, "points_layer")
arcpy.MakeFeatureLayer_management(points2, "points_layer2")

with arcpy.da.SearchCursor(points,["stacja","nazwa"]) as point_cursor:
    for i in point_cursor:
        where_clause = "stacja={}".format(i[0])
        with arcpy.da.SearchCursor(points2, ["stacja","nazwa"], where_clause) as cur:
            arcpy.SelectLayerByAttribute_management("points_layer2","NEW_SELECTION",""" "stacja"= {} """.format(i[0]))
  • 2
    Your second (internal) SearchCursor appears to be completely unused. Why is it even there? Delete that line (and the where_clause = line before it), fix the indenting, and the script behaviour would be unchanged. Commented Aug 9, 2021 at 4:48
  • 2
    What is the script doing that you don't want it to do? Or what is it NOT doing that you do want it to do? If it produces an error, then copy/paste the full error stack into your question (edit your original question and add it in there). Commented Aug 9, 2021 at 4:53
  • 1
    Please don't delete and repost questions. Besides losing any comments and edit history, you're also beginning a game of "chicken" with the automated spam defense systems which will lock your account for abuse. Above and beyond that, you are violating the "Avoid nesting cursors" rule, and haven't actually used them correctly, either (no row iteration on the inner loop, and poor variable name choice on the outer). This may be an XY Problem, where need to ask about the task, and not your methodology.
    – Vince
    Commented Aug 9, 2021 at 13:14

1 Answer 1


I think what you are trying to do, or at least most of what I think you are trying to do, can be done using a single tool, Split By Attributes, which:

Splits an input dataset by unique attributes.

  • Yes, something like this, but directly to shp, not table. So I decided use Select by Attribute. I don't want to use the for loop on the second cursor because it has 10,000 rows of rentals from each station, because they will double.
    – Mab
    Commented Aug 9, 2021 at 8:06
  • I'm confident that you should not need a second cursor, and maybe not even a first cursor. I think you should try using Split By Attribute as an alternative workflow, and let us know where it falls short. I think you're likely to get far more help on that path than doing it the way you're currently trying to.
    – PolyGeo
    Commented Aug 9, 2021 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.