Linked Questions
45 questions linked to/from Is there a simple way to delete a list element by value?
23 votes
2 answers
113k views
Python. How to remove zeroes from a list in Python [duplicate]
X = [0,5,0,0,3,1,15,0,12] for value in range(0,len(X)): if X[value] <= 0: del X[value] print(X) print(X) I run the code but then i get an error saying that the list is out of ...
10 votes
1 answer
10k views
remove item from list according to item's special attribute [duplicate]
I have a list composed with my defined items, each of which has a attribute .name t = [item1, item2] I want to remove item from the t list according to their attribute .name, like remove() or pop() ...
4 votes
2 answers
3k views
How to delete an element from a list while iterating over it in Python? [duplicate]
Given a list of numbers: L = [1, 2, 3, 4, 5] How do I delete an element, let's say 3, from the list while I iterate over it? I tried the following code but it didn't do it: for el in L: if el == ...
1 vote
1 answer
3k views
Loop through json file and delete key if it matches [duplicate]
I have the following structure on a JSON file: { "channels": [ "180873781382873088", "181268808055521280", "183484852287307777", "174886257636147201", "174521530573651968" ] } ...
-3 votes
2 answers
2k views
How to take out single element out of list in python? [duplicate]
I have list as follows: list = [u'windows 32bit', u'i7', u'5Mb', u'energenie_eu', u'nvidia_970'] I want to take out value '5Mb' out of it. So, suggest me how can i take out value '5Mb' seperated from ...
2 votes
2 answers
735 views
Delete item in a list in Python [duplicate]
How to find and delete only one element in a list in Python? # Example:deleting only the first (1,2) in the list a = [(4, 5), (1, 2), (7, 5), (1, 2), (5, 2)] # result expected a = [(4, 5), (7, 5), (1,...
1 vote
3 answers
863 views
How to remove all elements of five from a list [duplicate]
My code for this returns 'None'. In case my question is not clear if i take the list [1 , 3 , 4 , 5 ,5 , 7], I want the list [1 , 3 , 4 , 7] to be returned. My code is as follows: print("This program ...
1 vote
4 answers
371 views
Python: Deeply nested dictionary editing [duplicate]
I have the following situation: data = {'key1' : 'value1', 'key2': [ {'subkey1': 'subvalue1', 'subkey2': 'subvalue2'}, {other ...
0 votes
2 answers
109 views
How do I print the difference of a tuple in python [duplicate]
I want to output difference two tuple and remove one element on tuple a = [(1,2),(2,3),(3,3)] if (1,2) in a: ## how to remove (1,2) on tuple i need output [(2,3),(3,3)] how to do it? Thanks,
-1 votes
1 answer
190 views
Instance of a class not being destroyed with del [duplicate]
I'm trying to delete an object (in this case, and instance of a Fruit class) when it intersects with the head of my snake. The problem is that when it detects a fruit object, del doesn't appear to do ...
-2 votes
1 answer
72 views
How to remove a specific object in a list? [duplicate]
For example I have a list that looks like this: l = [(1,2),(1,3),(4,1)] how can I remove item (1,3) without knowing the index of the item? I tried l.pop((1,3)) but I got this error message: ...
-1 votes
2 answers
69 views
How to delete something from a list? [duplicate]
I am trying to write a Suduoku game. Every time I use a number, I need to delete it from the list. I have already tried .pop[1], but it did nothing when the number was one. import random rownum1 = ...
0 votes
2 answers
50 views
How can I remove a element from a list even though there are equal elements inside? [duplicate]
For example: list = [1,7,2,8,9,2,7,9,2,8,2] How can I remove two of the "2" without removing the others?
0 votes
0 answers
29 views
Is there a reason why you can't use the pop() method by element/value name in lists? [duplicate]
Python beginner just wondering why is it you're not able to pop() an item from a list by using it's value within a list, instead having to know it's index? e.g. colours = ["blue", "...
232 votes
5 answers
173k views
Are lists thread-safe?
I notice that it is often suggested to use queues with multiple threads, instead of lists and .pop(). Is this because lists are not thread-safe, or for some other reason?