Python Forum
I cannot delete and the elements from the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I cannot delete and the elements from the list
#1
Hello,
I am trying to run this code. The idea is to compare all elements in the list with another single element. If they are same, process and delete and return the last value. If they are not same just delete:
But I have some troubles in delete part. Here is my code:

def func(queue):
    for index, q in enumerate((queue.copy())): #queue[:]: 
                   if q.idt==OP[1]:
                       a=Operation(q,OP[0])
                       return self.queue.pop(index)
                       #return self.queue.remove(q)
                   elif q.idt < OP[1]:
                       self.unpauli.append(q.idt)
                       self.queue.pop(index)
                       #self.queue.remove(q)
                       #continue
                      
I have no error message but the result is coming wrong. Where is the mistake in the code?
Reply
#2
Python documentation, The return statement:

Quote:return may only occur syntactically nested in a function definition, not within a nested class definition.

Function is nowhere to be seen.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(May-11-2021, 10:51 AM)perfringo Wrote: Python documentation, The return statement:

Quote:return may only occur syntactically nested in a function definition, not within a nested class definition.

Function is nowhere to be seen.

I just did not paste the beginning of the function but it is not a problem. THe problem is that I cannot delete my elements from the list. I tried a small program for seeing what is going on here and the result is same:
l2=[0,1,2,3,4,5,6,7,8,9]
a=1
l2.copy()
for ind,el in enumerate(l2):   
    if a==el:
       l2.remove(ind)
    else:
       #l2.pop(ind)
       l2.remove(ind)
print(l2)     
it should return an empty list but no !
Reply
#4
Don't modify an iterator while you're iterating over it,it's a known bad patternđź’€
Will mess up list index and will get your result.
Could add enumerate(l2[:]) to make it work,but still no t good as as remove() has to go over the whole list for every iteration O(n^2).

The solution is to make a new list,then therew is no remove()(that rarely ever need to be used) used in the loop.
l2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = 1
result = []
for ind, el in enumerate(l2):
    if el != a:
       result.append(el)

print(result)
[0, 2, 3, 4, 5, 6, 7, 8, 9]
Or list comprehension.
>>> l2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [el for el in l2 if el != 1]
[0, 2, 3, 4, 5, 6, 7, 8, 9]
quest likes this post
Reply
#5
"If fact, in any programming language for most part if you mutate something while you iterating over it you living in state of sin and you deserve whatever happens to you" -- Raymond Hettinger, Python core-developer, Transforming Code into Beautiful, Idiomatic Python

What is purpose of l2.copy(). It creates copy but as you don't bind it to the name so it's not accessible, no pointers to it and garbage collector will destroy it. It doesn't have any effect on result of your code.

>>> help(list.copy)
Help on method_descriptor:

copy(self, /)
    Return a shallow copy of the list.
quest likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why am I getting list elements < 0 ? Mark17 8 483 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 264 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 390 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 387 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Delete list while iterating shantanu97 1 396 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  List of lists - merge sublists with common elements medatib531 1 801 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Find Common Elements in 2 list quest 4 789 Apr-14-2021, 03:57 PM
Last Post: quest
  Sorting Elements via parameters pointing to those elements. rpalmer 3 789 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Group List Elements according to the Input with the order of binary combination quest_ 19 1,732 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Adding List Element if Second part of the List Elements are the Same quest_ 3 751 Nov-25-2020, 04:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020