Modify list while iterating python

From the course: Python Data Science Mistakes to Avoid

Video is locked.

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

Start my 1-month free trial Buy this course [703,000.00*]
  • 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.

  • Exercise File: Subscribe to access. Ex_Files_Python_Data_Mistakes.zip
Download the exercise files for this course. Get started with a free trial today.

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.

Watch this course anytime, anywhere. Get started with a free trial today.

Contents

  • Introduction Introduction
    • Avoiding common Python mistakes
      39s
    • Getting the most from this course
      55s
  • 1. Avoid Mistakes in Coding Practices 1. Avoid Mistakes in Coding Practices
    • Not writing comments
      3m 11s
    • Not organizing your directory
      3m 11s
    • Not testing
      2m 36s
    • Not sharing data referenced in code
      1m 10s
    • Hard coding inaccessible paths
      3m 10s
    • Name clashing with Python standard library
      2m 26s
    • Not importing relevant libraries and modules
      43s
    • Naming vaguely
      1m 51s
  • 2. Avoid Mistakes in Structuring Code 2. Avoid Mistakes in Structuring Code
    • Modifying a list while iterating over it
      2m 14s
    • Using for loops instead of vectorized functions
      3m 48s
    • Using class variables vs. instance variables
      4m 2s
    • Calling functions before defining
      1m 40s
    • Creating circular dependencies
      1m 34s
  • 3. Avoid Mistakes in Handling Data 3. Avoid Mistakes in Handling Data
    • Not choosing the right data structure
      2m 22s
    • Skimming data
      2m 1s
    • Not using the right visualization type
      1m 15s
    • Not addressing outliers
      1m 23s
    • Not updating your dataset
      1m 30s
    • Not cleaning data
      1m 20s
  • 4. Avoid Mistakes in Machine Learning 4. Avoid Mistakes in Machine Learning
    • Using features that will be unavailable later
      1m 31s
    • Using redundant features
      1m 45s
  • Conclusion Conclusion
    • Get started with Python
      1m 7s

Video liên quan

Chủ Đề