From the course: Python Data Science Mistakes to Avoid
Unlock the full course today
Join today to access over 17,300 courses taught by industry experts or purchase this course individually.
Modifying a list while iterating over it - Python Tutorial
From the course: Python Data Science Mistakes to Avoid
- Transcripts
- Exercise Files
- View Offline
Modifying a list while iterating over it
- [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
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
- Ex_Files_Python_Data_Mistakes.zip
Download courses and learn on the go
Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.
Contents
-
Introduction Introduction
-
Avoiding common Python mistakes39s
-
Getting the most from this course55s
-
-
1. Avoid Mistakes in Coding Practices 1. Avoid Mistakes in Coding Practices
-
Not writing comments3m 11s
-
Not organizing your directory3m 11s
-
Not testing2m 36s
-
Not sharing data referenced in code1m 10s
-
Hard coding inaccessible paths3m 10s
-
Name clashing with Python standard library2m 26s
-
Not importing relevant libraries and modules43s
-
Naming vaguely1m 51s
-
-
2. Avoid Mistakes in Structuring Code 2. Avoid Mistakes in Structuring Code
-
Modifying a list while iterating over it2m 14s
-
Using for loops instead of vectorized functions3m 48s
-
Using class variables vs. instance variables4m 2s
-
Calling functions before defining1m 40s
-
Creating circular dependencies1m 34s
-
-
3. Avoid Mistakes in Handling Data 3. Avoid Mistakes in Handling Data
-
Not choosing the right data structure2m 22s
-
Skimming data2m 1s
-
Not using the right visualization type1m 15s
-
Not addressing outliers1m 23s
-
Not updating your dataset1m 30s
-
Not cleaning data1m 20s
-
-
4. Avoid Mistakes in Machine Learning 4. Avoid Mistakes in Machine Learning
-
Using features that will be unavailable later1m 31s
-
Using redundant features1m 45s
-
-
Conclusion Conclusion
-
Get started with Python1m 7s
-