-3

TypeError: 'employee' object cannot be interpreted as an integer

I AM GETTING THIS TYPE OF ERROR

name = input("Enter name you want to delete : ")
    for i in lst:
        if name in i.name:
            lst.pop(i)
            print("Employee deleted!")

I was expecting that the object would get deleted But it Showing type error TypeError: 'employee' object cannot be interpreted as an integer

  • 1
    `list.pop` accepts an index, not an object to delete. If you want to pass an actual object, use `list.remove`. Also, don't remove items from the list while iterating through it. – matszwecja Nov 24 '22 at 10:01
  • is 'lst' a list of class objects ? – Piero Costa Nov 24 '22 at 10:02
  • We don't even know what `lst` is. You should post a [mre]. Anyway, did you check about [`list.pop`](https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types) before using it? The argument is expected to be the ***index*** of the object to delete, not the object – Tomerikoo Nov 24 '22 at 10:03

1 Answers1

0

From python documentation the method list.pop except an integer as parameter with give the position of the element to remove in the list.

From your code, the list lst seems to contain objects of the employee class and this isn't an integer. For your case, the list.remove function will be the one to use since it work with the objects inside the list rather than the position in it. You should also implement the __eq__ method in your employee class if it's not already done, this is what python will use to compare the employee object in your list and the one to delete.

A last point, if you really want to use the list.pop method instead of list.remove you can do it by using list.index first to get the position of the object to remove from the list.

lst.pop(lst.index(i))
Xiidref
  • 1,456
  • 8
  • 20
  • That is not a good suggestion as removing items while iterating on the list will create unwanted side-effects. See [this answer](https://stackoverflow.com/a/1207427/6045800) – Tomerikoo Nov 24 '22 at 10:16