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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:26+00:00 2026-06-17T18:28:26+00:00

a = [0,1,2,3,4,5] for b in a: print :+str(b) a.pop(0) Thinking that this would

  • 0
a = [0,1,2,3,4,5]
for b in a:
  print ":"+str(b)
  a.pop(0)

Thinking that this would work in order going through the entire list and all its items I ran this code and expected this.

:0
0
:1
1
:2
2
:3
3
:4
4
:5
5

Instead I got this:

:0
0
:2
1
:4
2

Now I understand why this happened but is this an error in python? Shouldn’t it still go through all of the original objects instead of the length of the current list? And why did this not throw and out of bounds error?
IE: Shouldn’t it still have done:

:0
0
:1
2
:2
4
:3
Error
:4
Error
:5
Error
  • 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-17T18:28:27+00:00Added an answer on June 17, 2026 at 6:28 pm

    You are looping over a list and altering it at the same time. By using .pop() you are shortening the list, but the iterator pointer is not updated.

    Use a copy instead:

    for b in list(a):
    

    or

    for b in a[:]:
    

    where the [:] slice notation returns a list copy.

    Another approach would be to use a while loop instead:

    while a:
        print a.pop(0)
    

    because an empty list tests as boolean False.

    The python for loop uses it’s argument as an iterator, it does not itself keep an index. There is no way for the for loop to ‘know’ you removed elements. Instead, it’s the list() iterator that keeps that pointer:

    >>> a = [0,1,2,3,4,5]
    >>> itera = iter(a)
    >>> itera.next()  # index 0 -> a[0] is 0
    0
    >>> a.pop(0)
    0
    >>> a
    [1,2,3,4,5]
    >>> itera.next()  # index 1 -> a[1] is 2
    2
    

    That iterator keeps a counter, and every time you call next() on the iterator it’ll give you the value at the next index, whatever that value may be, until the counter is equal to the current lenght of the list.

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

Sidebar

Related Questions

I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
I want to do something like this... def helloWorld(): print Hello world! str.helloWorld =
I've got a list of dates (datetimes): >>> print str(range) [datetime.datetime(2011, 12, 15, 0,
This code - for ss_key in sd.keys(): print str(sd[ss_key]) + ' ' + ss_key
I have the following: rule = http://www.abc.com/ test = http://www.abc.com/test print(str(re.compile(rule).match(test))) I want this
class a(str): def b(self,*x,**y): print str.decode(self,*x,**y) b=a() b.b('utf-8','aaa') # This prints nothing, why?
This is my test code, test.py : str = input(IP: ) print(str) When running
Why is this: # other stuff... print str(checksum)+ +ranswer+.0000000000 giving syntax error in Python
Here is a very simple program: a = [[]]*3 print str(a) a[0].append(1) a[1].append(2) a[2].append(3)
If I do: print The item is: + str(1) + . I will get:

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.