Delete all objects in a list

tl;dr; mylist.clear() # Added in Python 3.3 del mylist[:] are probably the best ways to do this. The rest of this answer tries to explain why some of your other efforts didn’t work. cpython at least works on reference counting to determine when objects will be deleted. Here you have multiple references to the same …

Read more

Which is better in python, del or delattr?

The first is more efficient than the second. del foo.bar compiles to two bytecode instructions: 2 0 LOAD_FAST 0 (foo) 3 DELETE_ATTR 0 (bar) whereas delattr(foo, “bar”) takes five: 2 0 LOAD_GLOBAL 0 (delattr) 3 LOAD_FAST 0 (foo) 6 LOAD_CONST 1 (‘bar’) 9 CALL_FUNCTION 2 12 POP_TOP This translates into the first running slightly faster …

Read more