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

  • Home
  • SEARCH
  • 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 8114873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:17:10+00:00 2026-06-06T03:17:10+00:00

I am trying to iterate through a list checking every string in the list

  • 0

I am trying to iterate through a list checking every string in the list for a character.

test = [str(i) for i in range(100)]

for i in test:
    if '0' or '4' or '6' or '8' in str(i):
        test.remove(i)

I thought this would be fine but, the list is this after:

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

Where the ‘2’ is removed but, the ’41’ is not? I’ve noticed its only even numbers but, don’t know why.

  • 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-06T03:17:11+00:00Added an answer on June 6, 2026 at 3:17 am

    You have two problems. First, this if statement is always taken.

    if '0' or '4' or '6' or '8' in str(i):
    

    Because the string 0 is a non-zero-length string, and is therefore True. 0 or 4 is 0 and therefore True. The rest of the statement doesn’t matter.

    I expect you actually wanted to test whether each of those digits was in the string representation of the integer. As you have it written now, you are only testing whether 8 is in the string, and that’s not even getting tested because the expression evaluates to True before it even gets there. Something like this would work:

    if any(x in i for x in '0468'):
    

    By the way, str(i) is superfluous because your list is already a list of strings.

    The other problem is that you are deleting items from the list you are iterating over. So, here’s what happens:

    • The first item, 0, is removed, because your if statement is always taken.
    • The second item, 1, becomes the first item, because you removed 0.
    • The for loop goes on to the second item, which is now 2.

    In other words, because you deleted 0, 1 is never tested. Thus, every other item (all the even numbers) are removed.

    The easiest way to avoid this in your code is to iterate over a copy of the list:

    for i in test[:]:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to iterate through a list, and I need to perform specific
I am trying to iterate through a list of float widths that varies. [10.5,
I am trying to iterate through a list and take each part of the
So I'm trying to iterate through a list of files that are within a
I'm trying to iterate through a list of folders, but I can't seem to
Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull
I am trying to iterate through a list of keys from a hash table
I have a list within a list, and I am trying to iterate through
to get right down to it, I'm trying to iterate through a list of
I am trying to iterate through a list of servers & passwords to change

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.