Removal of element from list while traversing causes the next element to be skipped
Matthew_WARREN at bnpparibas.com
Matthew_WARREN at bnpparibas.com
Thu Jan 31 12:45:42 EST 2008
Hmm, how does this fare??
for i in range(len(a)):
if a[i]==99: a=a[:i]+a[i+1:]
I like following your guys code noodling. I can come up with something
that does what it appears your doing, sometimes, but as to it's relevant
merits I havent a clue :)
matt.
Internet
mr.cerutti at gmail.com
To
python-list
Sent by: cc
python-list-bounces+matthew.warren=uk.bnpparibas.com@
python.org Subject
Re: Removal of element from list while
30/01/2008 13:04 traversing causes the next element to be
skipped
On Jan 30, 2008 6:57 AM, Arnaud Delobelle <arnodel at googlemail.com> wrote:
> Or one could use the trick of counting from the right (untested):
>
> n = len(a)
> for i, x in enumerate(a):
> if x == 99: del a[i-n]
Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet:
i = 0
while i < len(a):
if a[i] == 99:
del a[i]
else:
i += 1
--
Neil Cerutti <mr.cerutti+python at gmail.com>
--
http://mail.python.org/mailman/listinfo/python-list
This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.
---------------------------------------------
Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
More information about the Python-list
mailing list