>>> mylist = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']
>>> for x in mylist:
	print '--------------'
	print x
	if x[0] == 'a':
		print "Removing ",x
		mylist.remove(x)

		
--------------
bunnies
--------------
apples
Removing  apples
--------------
fruit
--------------
amsterdam
Removing  amsterdam
>>> print mylist
['bunnies', 'animals', 'fruit']

The output should be bunnies and fruit, animals should have been removed because it starts with an a...

I'm very confused why this isn't working, but I'm assuming that it has something to do with looping through a list while removing parts of it... Does anybody have a clearer answer for this?

Recommended Answers

All 4 Replies

Here i make a copy of the list,then iterating over it.
It is not a good ideé to delete/add somthing from a list you are iterating over.
The solution is to make a copy or new list.

mylist = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']

for item in mylist[:]:
	if item[0] == 'a':
            mylist.remove(item)
print mylist

'''
output--->
['bunnies', 'fruit']
'''

This maybe easyer to read.

lst = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']
    
new_lst = []
for item in lst:
        if item[0] != 'a':
            new_lst.append(item)
print new_lst

'''
output--->
['bunnies', 'fruit']
'''

Or maybe a more elegant solution with a function

def list_remove(item):   
        if item[0] == 'a':
            return False
        return True
    
lst = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']
lst1 = [item for item in lst if list_remove(item)]
print lst1

'''
output--->
['bunnies', 'fruit']
'''
Member Avatar for sravan953

Hey, I too was totally confused by your problem, it happened to me too! But, I tried this:

>>> mylist = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']
>>> for x in mylist:
	print '--------------'
	print x
	if x[0] == 'a':
		print "Removing ",x
		mylist.remove(x)

		
--------------
bunnies
--------------
apples
Removing  apples
--------------
fruit
--------------
amsterdam
Removing  amsterdam
>>> print mylist
['bunnies', 'animals', 'fruit']
>>> for x in mylist:
	print '--------------'
	print x
	if x[0] == 'a':
		print "Removing ",x
		mylist.remove(x)

		
--------------
bunnies
--------------
animals
Removing  animals
>>>

All I did was run the for loop twice, and it worked, but I still have no idea why this is happening! :?:

Like snippsat so wisely said,
"do not change a list that your are iterating over"
this will upset the indexing of the list during the iteration process.

snippsat also recommended to iterate over a copy of mylist:

mylist = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam']
mylist_copy = list(mylist)  # or mylist[:]
for x in mylist_copy:
    print '--------------'
    print x
    if x[0] == 'a':
        print "Removing ",x
        mylist.remove(x)

print mylist

"""
my output -->
--------------
bunnies
--------------
apples
Removing  apples
--------------
animals
Removing  animals
--------------
fruit
--------------
amsterdam
Removing  amsterdam
['bunnies', 'fruit']
"""

Very interesting little caveat there. But, thanks all for the helpful code snippets. Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.