Enumerating iterators Python 3


show more Enumerating iterators provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Enumerating iterators

For loops in Python are very useful for iterating over container types, and yet they are significantly different from for loops in other languages. In particular, for loops in other languages tend to have some sort of an index variable associated with them, and this can some times be useful. And we don't have that in the standard for loop in Python. So, let's take a look at how we can get that very easily using the enumerate function. Go ahead and make a working copy of for.py. I'll call it for-working.py and open that working file, and this is our little readlines example. And if we run this, You'll notice that we get the lines of text from the lines.txt file, which looks like this.

They have that extra new line in them. We'll go ahead and get rid of that with end=" ". I am going to add something to this here. I am going to add the enumerate function, enumerate, and it goes like that. Enumerate actually returns two values. The first value is an index, and th...

Enumerating iterators
Video duration: 3m 22s 6h 36m Beginner

Viewers:

Enumerating iterators provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...