Loops For Objects

You will learn how to loop over the different types of objects available in Python in this lesson.

Looping over lists

If you want to do something with every element of a list, you can use a for loop. A simple for loop looks like this:

for e in my_list:
 print(e)

If you need to work with not only the elements in the list, but also their position in the list, you can use a more complicated version of the for loop:

for i in range(0, len(my_list)):
 print(my_list[i], "is at", i)

If your background is in C, C++, or Java, you may be tempted to use the more complicated version for everything. However, that’s not helpful. Each version has its own uses.

The following is an example of both of these for loop versions:

Get hands-on with 1400+ tech skills courses.