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?
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): ...
121 votes
9 answers
130k views
What is the most pythonic way to pop a random element from a list?
Say I have a list x with unkown length from which I want to randomly pop one element so that the list does not contain the element afterwards. What is the most pythonic way to do this? I can do it ...
111 votes
12 answers
28k views
Strange result when removing item from a list while iterating over it in Python
I've got this piece of code: numbers = list(range(1, 50)) for i in numbers: if i < 20: numbers.remove(i) print(numbers) But, the result I'm getting is: [2, 4, 6, 8, 10, 12, 14, 16, ...
140 votes
3 answers
140k views
What is wrong with using a bare 'except'?
I tried making a function to check if an image is displayed on the screen using PyAutoGui and came up with this: def check_image_on_screen(image): try: pyautogui.locateCenterOnScreen(...
7 votes
4 answers
59k views
How to remove a tuple from the list
Here is my list of tuple: [('Abbott', 'Texas'), ('Abernathy', 'Texas'), ('Abilene', 'Texas'), ('Ace', 'Texas'), ('Ackerly', 'Texas'), ('Alba', 'Texas'),('Addison', 'Texas'), ('Adkins', 'Texas'), ('...
9 votes
3 answers
11k views
subprocess.check_output with grep command fails when grep finds no matches
I'm trying to search a text file and retrieve lines containing a specific set of words. This is the code I'm using: tyrs = subprocess.check_output('grep "^A" %s | grep TYR' % pocket_location, shell = ...
0 votes
3 answers
6k views
How to remove Specific values in python list
result = [(u'ABC', u'(Choose field)', u'ABCD', u'aa', u'A', u'A_100')] I'm trying to remove '(Choose field)' from the above list using the following syntax: result.remove('(Choose field)') # and ...
2 votes
2 answers
3k views
Getting rid of 'nan' from list value of dictionary , python
I have this dictionary: dict_new = {'extracted_layout': [nan, nan, nan, nan, nan, nan, nan, nan, nan, 'shyamanna layout', nan, nan, nan, nan, 'm t s layout', nan, nan, nan, nan, nan, nan, nan, nan, ...
1 vote
5 answers
2k views
How can I remove an object from array in python?
How can I remove an object from array in python? I want to select an msgID and want to delete that full object with username, msg, time_stamp (all of it). room_msg = [ { 'msgID': 1, 'username'...
-6 votes
2 answers
4k views
How to to remove all zeros from a list
I want to remove all zeros from the list after sorting in descending order. for x in range (1,count): exec("col"+str(x) + "=[]") with open (xvg_input, 'r') as num: line_to_end = num....
-1 votes
2 answers
1k views
How can I remove a certain element from an array?
I have a Python array. arr = [1, 2, 3, 4, 5] I want to remove N with a certain condition from the arr and store it again in the arr. The condition: the remainder divided by 2 is 0 (it can be any ...
1 vote
3 answers
741 views
Delete a specific value from multi-value dictionary in python
``I have a dictionary in python like this. dictionary = {"00":[1,2,3,4,5,6,7,8,9],"01":[1,2,3,4,5,6,7,8,9],"02":[1,2,3,4,5,6,7,8,9],"03":[1,2,3,4,5,6,7,8,9],"04":[1,2,3,4,5,6,7,8,9]........up-to "99":...
0 votes
1 answer
581 views
GroupBy Value in DataFrame and getting a list of words seperated by comma
I have a pandas dataframe as shown here. There are many more columns in that frame that are not important concerning the task. id pos value sente 1 a I 21 2 b ...
0 votes
2 answers
440 views
How to remove an item from a list in python with substring match
I have got a list l1 = ['00001MMYYYSSSS', '00002YYSSMMYNNN', '00003FFMMNNNSS'] and another list l2 = ['00001', '00003']. I need to remove the items at index 0 and 2 in the list l1 as it contains the ...
0 votes
0 answers
541 views
Why does np.array falsely claim that my array has an inhomogeneous part?
I have a question that is similar (but not the same) to this one: The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (33,) + inhomogeneous part When I run this ...