Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:05:31+00:00 2026-06-03T14:05:31+00:00

This is the most common problem I face while trying to learn programming in

  • 0

This is the most common problem I face while trying to learn programming in python. The problem is, when I try to iterate a list using “range()” function to check if given item in list meets given condition and if yes then to delete it, it will always give “IndexError”. So, is there a particular way to do this without using any other intermediate list or “while” statement? Below is an example:

l = range(20)
for i in range(0,len(l)):
  if l[i] == something:
    l.pop(i)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:05:32+00:00Added an answer on June 3, 2026 at 2:05 pm

    First of all, you never want to iterate over things like that in Python. Iterate over the actual objects, not the indices:

    l = range(20)
    for i in l:
        ...
    

    The reason for your error was that you were removing an item, so the later indices cease to exist.

    Now, you can’t modify a list while you are looping over it, but that isn’t a problem. The better solution is to use a list comprehension here, to filter out the extra items.

    l = range(20)
    new_l = [i for i in l if not i == something]
    

    You can also use the filter() builtin, although that tends to be unclear in most situations (and slower where you need lambda).

    Also note that in Python 3.x, range() produces a generator, not a list.

    It would also be a good idea to use more descriptive variable names – I’ll presume here it’s for example, but names like i and l are hard to read and make it easier to introduce bugs.

    Edit:

    If you wish to update the existing list in place, as pointed out in the comments, you can use the slicing syntax to replace each item of the list in turn (l[:] = new_l). That said, I would argue that that case is pretty bad design. You don’t want one segment of code to rely on data being updated from another bit of code in that way.

    Edit 2:

    If, for any reason, you need the indices as you loop over the items, that’s what the enumerate() builtin is for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is the most common problem in the history of programming but
I'm now facing the most common problem faced my many while working with listboxes.
I believe this might be one of the most common problem that users faces
This probably isn't the most common filename parsing problem, but I have a program
I have this query to calculate the most common days of the week and
I am having this most annoying issue when I try to rebuild my solution
I've been working on this problem most of the morning and I think I
I'm pulling in xml using the cocoa NSXmlParser. Since this process most likely won't
I'll let this jsfiddle describe most of the problem for me. http://jsfiddle.net/zAPVQ/4/ I've got
Although this is a fairly common problem, I am struggling with what the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.