Linked Questions
878 questions linked to/from How to remove items from a list while iterating?
128 votes
9 answers
194k views
Python: Removing list element while iterating over list [duplicate]
I'm iterating over a list of elements in Python, do some action on it, and then remove them if they meet certain criteria. for element in somelist: do_action(element) if check(element): ...
139 votes
6 answers
333k views
How to remove list elements in a for loop in Python? [duplicate]
I have a list a = ["a", "b", "c", "d", "e"] I want to remove elements in this list in a for loop like below: for item in a: print(item) a....
57 votes
14 answers
48k views
Elegant way to remove items from sequence in Python? [duplicate]
When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items ...
67 votes
4 answers
184k views
Removing elements from a list containing specific characters [duplicate]
I want to remove all elements in a list which contains (or does not contain) a set of specific characters, however I'm running in to problems iterating over the list and removing elements as I go ...
15 votes
4 answers
16k views
Remove elements as you traverse a list in Python [duplicate]
In Java I can do by using an Iterator and then using the .remove() method of the iterator to remove the last element returned by the iterator, like this: import java.util.*; public class ...
4 votes
4 answers
8k views
Modifying a list while iterating over it - why not? [duplicate]
Almost every tutorial and SO answer on this topic insists that you should never modify a list while iterating over it, but I can't see why this is such a bad thing if the code is valid. For example: ...
11 votes
4 answers
21k views
problem Deleting list items in a for loop (python) [duplicate]
Possible Duplicate: Remove items from a list while iterating in Python Hi im having a problem i cant seem to delete items whilst iterating through a list in python, Here is what i've got: a title ...
2 votes
5 answers
64k views
(Python) List index out of range - iteration [duplicate]
for i in range(len(lst)): if lst[i][0]==1 or lst[i][1]==1: lst.remove(lst[i]) return lst This gives "IndexError: list index out of range" Why is this happening?
11 votes
5 answers
10k views
Best method for changing a list while iterating over it [duplicate]
I have several instances in a python script (v2.6) where I need to modify a list in-place. I need to pop values from the list in response to interactive input from the user and would like to know the ...
3 votes
2 answers
13k views
Remove element from list when using enumerate() in python [duplicate]
Object is a decoded json object that contains a list called items. obj = json.loads(response.body_as_unicode()) for index, item in enumerate(obj['items']): if not item['name']: obj['...
5 votes
4 answers
19k views
Delete item from list in Python while iterating over it [duplicate]
I'm writing a round robin algorithm for a tournament app. When the number of players is odd, I add 'DELETE' to the list of players, but later, when I want to delete all items from schedule list that ...
2 votes
6 answers
20k views
Find second maximum number in a list [duplicate]
First line contains N. Second line contains list of N integers each separated by a space. I need to find the second largest number in list. My code: N = int(raw_input()) L = map(int, raw_input().split(...
8 votes
3 answers
20k views
remove() returns None when I try to remove a variable from a list [duplicate]
I am working on a part of a program that turns a statement into a question. When I try to remove x it returns None. I want it to print the sentence with that item removed, what am I doing wrong? def ...
4 votes
1 answer
15k views
Remove single characters from list [duplicate]
I am having trouble understanding why my code is failing to work. I am trying to remove words from a list that are only one character in length: line = ['word','a','b','c','d','e','f','g'] for words ...
2 votes
6 answers
5k views
delete elements from list in python and avoid shifting [duplicate]
I have a list in python and every time an element satisfies a certain condition I remove the element. The problem is that the for cycle seems to be skipping some elements. I think it's because the ...