Another common mistake in programming is modifying a list while iterating over it. In this video, learn how to avoid this mistake by using the approach of list comprehension.
- [Instructor] Another common mistake in programming … is modifying a list while iterating over it. … To illustrate why this can be a problem … as well as how to avoid this, … I'll be walking you through an example. … Let's say I created a list containing the items zero, … one, two, three, four, and five, … and saved it in a variable named nums. … Also, I defined a function named is even, … which takes in an integer as input and returns … whether the given integer is even or not. … For example, … when is even as called on seven, … false is returned. … And when is even as called on eight, … true is returned. … Afterwards, I wrote a for loop … that iterates over the items in the list nums. … In each iteration, … if the current item is even, … it is deleted from nums. … When the cell is run, … I get an index error that says list index out of range. … As I deleted items from the list … while iterating over the list, … there came a point where I had reached the end of the list. … Meaning there were no items left to visit …
Skill Level Intermediate
1. Avoid Mistakes in Coding Practices
2. Avoid Mistakes in Structuring Code
3. Avoid Mistakes in Handling Data
4. Avoid Mistakes in Machine Learning
Using redundant features1m 45s
Get started with Python1m 7s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.