Note that for every iteration the index will be increased, we can access the list with the corresponding index. Then, we use this index variable to access the items of the list in order from 0 to 3, where 3 is the end of the list. We have passed a sequence of numbers in the range from 0 to len(courses) with the index. Use the len() function to get the number of elements from the list/set object. Hence, use this to access an index in a for loop. Using the range()function you can get a sequence of values starting from zero. Similar to enumerate() function, we have other ways to access the index in a for loop. # Start loop indexing with non zero valueįor index, item in enumerate(courses,start=1): Let’s start loop indexing with a non-zero value by using start=1 param. You can change the start parameter to change the indexing. # Using enumerate() to access both index & itemĬourses=ġ.3 Start loop indexing with non-zero value This method combines indices to iterable objects and returns them as an enumerated object. When you use enumerate() with for loop, it returns an index and item for each element in a enumerate. 1.2 Use enumerate() to access index in a for loop
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |