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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:30:10+00:00 2026-06-14T18:30:10+00:00

I THINK DUPLICATE FROM Remove items from a list while iterating where best solution

  • 0

I THINK DUPLICATE FROM

Remove items from a list while iterating

where best solution is:

somelist[:] = [tup for tup in somelist if determine(tup)]

I don’t understand in deepth how it works, but seems to work !!!

NOW I understand how it works !! (2017/02/05)

I’m a python beginner and I find something maybe ot strange in list iteration and deletion. Here is my code:

# -*- coding: utf-8 -*-s
#
# Fichier: testques.py
#

liste = [0,1,2,3,4,5,6,7,8,9]

cpt1=0
cpt2=0
cpt3=len(liste)

for i in liste:
    if i != 2:
        cpt1 += 1
    else:
        cpt2 += 1
        del liste[i]

print cpt1
print cpt2
print cpt3

>python test.py
8
1
10

As you can see, 8+1 is not 10.

My thing is that “i” is only a reference to a list index. When I delete the element list, then all elements below the one I delete go up one level and then just next one is not parse by for … in.

Then can somebody help me to achieve the goal of having at the end the following result:
9
1
10

EDIT AFTER 3 ANSWERS
My goal is not counting number of elements equal to 2.

In fact, each element of my list is a dictionnary, and I want to delete all list items where the value for the xxx key of each dictionnary is no exactly 7 digits. And after I’ll do others operations on the now clean list. This is what i try to achieve.

Sorry for not havjng been enough clear in first question.

Here is my real code:

cpt = 0
liste_longueur_totale = len(i)
liste_longueur_clean = 0
liste_delete = 0
numero = re.compile('^[0-9]{7}$')
for row in i:
    if re.match(numero, row['j_numero']):
        liste_longueur_clean += 1
        cpt += 1
    else:
        liste_delete += 1
        print liste_delete, '-', i[cpt]['j_id'], '-', i[cpt]['j_numero']
        del i[cpt]
        cpt += 1
print "%s : %s + %s" % (liste_longueur_totale,liste_longueur_clean,liste_delete )
print len(i)

“i” is a list of 703 items. Each one is a dictionnary.

When I run it, I’ve the following result:

703: 673 + 15
688

When I run the same code without the line:

del i[cpt]

I’ve the following result:

703: 681 + 21
703

An in fact I know that 21 items are not responding to the re.match()

Then, I’m a little perplex !!!

  • 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-14T18:30:11+00:00Added an answer on June 14, 2026 at 6:30 pm

    Forget doing it the hard way. Especially if your list isn’t so long that iterating over it multiple times is a big deal.

    cpt1 = sum(1 for x in liste if x != 2)
    cpt2 = sum(1 for x in liste if x == 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python: How to remove all duplicate items from a list Hey guys I have
Possible Duplicate: Remove duplicates from a List<T> in C# i have a List like
I don't think my question is a duplicate of this one . what I
all I want to remove the duplicate value from this Array Array ( [0]
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Background Remove duplicate city names from a temporary table, based on the length of
Below is a simple function to remove duplicates in a list while preserving order.
Possible Duplicate: Removing leading zeroes from a string I need to remove leading 0s
Possible Duplicate: Windows Phone 7 Search Button I think that all Windows Phone mobiles
I think my question may be a duplicate, but I do not know what

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.