Linked Questions

23 votes
2 answers
113k views

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 ...
Andrews's user avatar
  • 239
10 votes
1 answer
10k views

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() ...
Liao Zhuodi's user avatar
  • 3,303
4 votes
2 answers
3k views

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 == ...
bodacydo's user avatar
  • 80.4k
1 vote
1 answer
3k views

I have the following structure on a JSON file: { "channels": [ "180873781382873088", "181268808055521280", "183484852287307777", "174886257636147201", "174521530573651968" ] } ...
Rodrigo Leite's user avatar
-3 votes
2 answers
2k views

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 ...
anamika email's user avatar
2 votes
2 answers
735 views

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,...
user3346439's user avatar
1 vote
3 answers
863 views

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 ...
J.Sharpe's user avatar
1 vote
4 answers
371 views

I have the following situation: data = {'key1' : 'value1', 'key2': [ {'subkey1': 'subvalue1', 'subkey2': 'subvalue2'}, {other ...
user avatar
0 votes
2 answers
109 views

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,
Ramin Farajpour Cami's user avatar
-1 votes
1 answer
190 views

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 ...
Python_plusSharp's user avatar
-2 votes
1 answer
72 views

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: ...
sam jack's user avatar
-1 votes
2 answers
69 views

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 = ...
Nathaniel Gamester's user avatar
0 votes
2 answers
50 views

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?
m4ntorrassdafasdf's user avatar
0 votes
0 answers
29 views

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", "...
Luke Gorman's user avatar
232 votes
5 answers
173k views

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?
lemiant's user avatar
  • 4,455
128 votes
9 answers
194k views

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): ...
Scrontch's user avatar
  • 3,472
121 votes
9 answers
130k views

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 ...
Henrik's user avatar
  • 14.5k
111 votes
12 answers
28k views

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, ...
Finger twist's user avatar
  • 3,806
140 votes
3 answers
140k views

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(...
CaioRamaglio's user avatar
  • 1,527
7 votes
4 answers
59k views

Here is my list of tuple: [('Abbott', 'Texas'), ('Abernathy', 'Texas'), ('Abilene', 'Texas'), ('Ace', 'Texas'), ('Ackerly', 'Texas'), ('Alba', 'Texas'),('Addison', 'Texas'), ('Adkins', 'Texas'), ('...
venkat's user avatar
  • 1,263
9 votes
3 answers
11k views

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 = ...
user3171031's user avatar
0 votes
3 answers
6k views

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 ...
Mohammad Ahmad Shabbir's user avatar
2 votes
2 answers
3k views

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, ...
Hypothetical Ninja's user avatar
1 vote
5 answers
2k views

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'...
Najmus Sakib's user avatar
-6 votes
2 answers
4k views

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....
Ron Ledger's user avatar
-1 votes
2 answers
1k views

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 ...
user avatar
1 vote
3 answers
741 views

``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":...
Gadheyan .t.s's user avatar
0 votes
1 answer
581 views

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 ...
Mi.'s user avatar
  • 510
0 votes
2 answers
440 views

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 ...
Navaneeth's user avatar
0 votes
0 answers
541 views

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 ...
Laulito's user avatar
  • 75

15 30 50 per page