Linked Questions
45 questions linked to/from Is there a simple way to delete a list element by value?
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
141k 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 ...