From the course: Get Ready for Your Coding Interview

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Two methods for Python list iteration

Two methods for Python list iteration

From the course: Get Ready for Your Coding Interview

Two methods for Python list iteration

- Now let's quickly go over two different ways to iterate over each element in a list in Python. Let's say we have our list A with the elements, item zero, item one, and item two. The first way iterate over each element in this list is this: for element in A, print element. And this would go through each item in this list and print it out. Item zero, item one, and item two. So that's the first method. To understand the second way to iterate over each element in a list, you first need to understand what the range function does. Range of three produces essentially lists with the elements zero, one, and two. It's not exactly a list, but it acts like it in this for-loop statement. "For I in range three", print I. This would print out zero, one, and two because we're just iterating over each element in this list-like object with the elements zero, one, and two. Using this, we can now iterate over each element in any of…

Contents