From the course: Learning ArcGIS Python Scripting

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Use loops to work with lists

Use loops to work with lists

- [Instructor] When you have a list of data that you want to run some code on, you can use a loop to step through that list and process each element one by one. A loop lets you execute a set of code over and over. For example, you can do something to all the feature classes in a geo database, or all the layers in a map, or all the text files in a folder. Loop blocks start with a specific line of code, for or while, and that line has to end with a colon. The next line and all the lines that are a part of that loop block, must be indented. It's the indention that determines what is a part of the loop. You end the loop by moving the next line of code back to the margin with no indention. So in this case, in this little example, the word "for" begins the loop and the line ends with a colon. And then the next two lines, the print and the count statement are both a part of the for loop, so they're repeated over and over again. But the final print statement, it's pulled back and lined up…

Contents