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 6077243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:41:57+00:00 2026-05-23T10:41:57+00:00

The following code: a = list(range(10)) remove = False for b in a: if

  • 0

The following code:

a = list(range(10))
remove = False
for b in a:
    if remove:
        a.remove(b)
    remove = not remove
print(a)

Outputs [0, 2, 3, 5, 6, 8, 9], instead of [0, 2, 4, 6, 8] when using Python 3.2.

  1. Why does it output these particular values?
  2. Why is no error given to indicate that underlying iterator is being modified?
  3. Have the mechanics changed from earlier versions of Python with respect to this behaviour?

Note that I am not looking to work around the behaviour, but to understand it.

  • 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-05-23T10:41:58+00:00Added an answer on May 23, 2026 at 10:41 am

    I debated answering this for a while, because similar questions have been asked many times here. But it’s just unique enough to be given the benefit of the doubt. (Still, I won’t object if others vote to close.) Here’s a visual explanation of what is happening.

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 0; remove? no
     ^
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 1; remove? yes
        ^
    [0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 3; remove? no
           ^
    [0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 4; remove? yes
              ^
    [0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 6; remove? no
                 ^
    [0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 7; remove? yes
                    ^
    [0, 2, 3, 5, 6, 8, 9]                <-  b = 9; remove? no
                       ^
    

    Since no one else has, I’ll attempt to answer your other questions:

    Why is no error given to indicate that underlying iterator is being modified?

    To throw an error without prohibiting many perfectly valid loop constructions, Python would have to know a lot about what’s going on, and it would probably have to get that information at runtime. All that information would take time to process. It would make Python a lot slower, in just the place where speed really counts — a loop.

    Have the mechanics changed from earlier versions of Python with respect to this behaviour?

    In short, no. Or at least I highly doubt it, and certainly it has behaved this way since I learned Python (2.4). Frankly I would expect any straightforward implementation of a mutable sequence to behave in just this way. Anyone who knows better, please correct me. (Actually, a quick doc lookup confirms that the text that Mikola cited has been in the tutorial since version 1.4!)

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

Sidebar

Related Questions

I've got the following code to output a list of items from amazon, but
Kinda knew to Python: I have the following code: def printCSV(output, values, header): 63
HI all, the following code does not getting executed, the code is class Invoice{
I have the following code that creates a list of dates between a range.
I've built a graph using the following code: G = networkx.Graph() G.add_edges_from([list(e) for e
I have the following code: List<T> list = new List<T>(); IEnumerable<T> query1 = ...
I have the following code: List<HtmlMeta> metas = new List<HtmlMeta>(); foreach (Control c in
Consider the following code: List<double> l = new List<double>(); //add unknown number of values
I have the following code: SPList list = web.Lists[this.ListName]; SPListItem item = list.Items.Add(); now
I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new

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.