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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:53:34+00:00 2026-05-23T04:53:34+00:00

I try to iterate over each item in the list. But iterator does not

  • 0

I try to iterate over each item in the list. But iterator does not go through all objects.
Here is my function code(dirs and root comes from os.walk)

def remove_hidden_dirs(dirs,root):
    """
    Function remove hidden directories . .. in unix
    """
    logging.debug("\n\n remove hidden" )
    logging.debug('Start removing hidden dirs for %s',root)
    logging.debug("Length of dirs %s",len(dirs))
    logging.debug('File list before removing')
    dirs.sort()
    for d in dirs:
        logging.debug(d)
    i=0
    for d in dirs:
        i=i+1
        logging.debug("Iterating over %s",d)
        if d[0:1] == '.' or d[0:2] =='..':
            dirs.remove(d)
            logging.debug("Dir %s is being removed because of being hidden dir",d)
        else:
            logging.debug("Dir %s is NOT being removed because of being hidden dir",d)
    logging.debug("Iterate on %s", i)
    logging.debug('File list after removing')
    for d in dirs:
        logging.debug(d)
    logging.debug('end remove hidden files\n\n')
    return dirs

And here is part of my log.file

DEBUG:root:Start removing hidden dirs for /home/artur/
DEBUG:root:Length of dirs 38
DEBUG:root:File list before removing
DEBUG:root:.adobe
DEBUG:root:.android
DEBUG:root:.cache
DEBUG:root:.config
DEBUG:root:.dbus
...
DEBUG:root:Iterate on 22 dirs
DEBUG:root:File list after removing
DEBUG:root:.adobe
DEBUG:root:.cache
DEBUG:root:.dbus
...

Thanks in advance for any help

  • 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-23T04:53:35+00:00Added an answer on May 23, 2026 at 4:53 am

    Very simple: you are removing the current item from dirs while iterating over it. That means you skip two forward on the next loop. See here.

    If what I said wasn’t clear, say you iterate over this list of numbers:

    [1, 2, 3, 4, 5, 6...]
     ^
    

    That’s the state of the list initially; then 1 is removed and the loop goes to the second item in the list:

    [2, 3, 4, 5, 6...]
        ^
    

    Since you’ve removed 1 and moved forward by one in the list, now you’re at 3, not 2.

    [2, 4, 5, 6...]
    

    Then you remove 3… and so on.

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

Sidebar

Related Questions

I got an array which I iterate over and try to create a variable.
I have scala map: attrs: Map[String , String] When I try to iterate over
I want to iterate over a list of DOM elements (check boxes) and keep
I'm wondering how to iterate over a List with mixed contents using foreach. See
I have an array: deleteIds= [User1]; and try to iterate over it as like:
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
Try to see which cast is faster (not necessary better): new c++ case or
I have a JavaScript function where someone can pass anything in, and I iterate
Good day. When i try to run the example from MSDN. But it failed
Is seems that the mmap interface only supports readline(). If I try to iterate

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.