473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deleting item of list while visting list...

10
I have strange issue when deleting element of list which I'm browsing. Here is the code , please have a look.
Expand|Select|Wrap|Line Numbers
  1. >>> li = ['mike','anil','jassi']
  2. >>> for name in li:
  3.     print name
  4.  
  5. mike
  6. anil
  7. jassi
  8. >>> for name in li:
  9.     print name
  10.     li.remove(name)
  11.  
  12. mike
  13. jassi
  14. >>> li
  15. ['anil']
  16.  
It seems strange, In my project , I have global list , while going thro' each item , I have to delete item upon certain condition. But I'm not getting result as expected whenever it delete element. It seems as upon deletion, List length get decreased and it is causing this behavior (might be As Designed from python or I can use list as this way)

If someone know details behind this behavior of list , pls share (However I got workaround for this issue by using another list and putting element and then deleting 1 by 1 :))

thanks,
Anil
Dec 4 '08 #1
2 2410
bvdet
2,851 Expert Mod 2GB
Python is doing exactly what you are telling it to do. The first iteration, 'mike' (element 0) is printed, then deleted. The next iteration, element 1 (now 'jassi') is printed, then deleted. The iteration is done at this point.
Dec 4 '08 #2
boxfish
469 Expert 256MB
As bvdet said, Python is not adjusting the indexes to account for deleted items. When you delete an item from the list, all the items after it move down one index. Your loop ends up skipping every other element. If you want delete all the items, you can use
Expand|Select|Wrap|Line Numbers
  1. for i in xrange(len(li)):
  2.     print li[0]
  3.     del li[0]
I hope this is helpful.
Dec 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
0
by: luckybird | last post by:
I succeeded in obtaining a CListCtrl item info using HitTest() when the List Ctrl is owned by my current thread. But when I tried HitTest an item of a List Ctrl owned by other threads such as...
5
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one...
25
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
1
by: Macca | last post by:
Hi, My app populates a list of custom objects from data fetched from a database. What i want to do now is filter the list and remove items from the list that don't match the filter criteria. ...
10
by: tkpmep | last post by:
For any list x, x.index(item) returns the index of the FIRST occurrence of the item in x. Is there a simple way to identify the LAST occurrence of an item in a list? My solution feels complex -...
7
by: GHZ | last post by:
Is this the best way to test every item in a list? def alltrue(f,l): return reduce(bool.__and__,map(f,l)) def onetrue(f,l): return reduce(bool.__or__,map(f,l)) False
15
by: buddhatown | last post by:
Very simple question for all you folks out there. I am total noob with js. I have a list called drawPathList thats just a list of xy coordinates. I use this to construct a drawing on a map. ...
2
by: james_027 | last post by:
hi, are there available library or pythonic algorithm for sorting a list of list depending on the index of the list inside the list of my choice? d_list = , , , ,
2
by: Steve | last post by:
I am working on a program that works like a check in/check out system. There is a folder on a network drive that stores a bunch of vb programs. This program will check in and check out programs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.