Questions tagged [del]

Short for delete, this is a keyword/command/function that deletes objects (Python, et al.) or files (Windows/DOS cmd / batch).

Short for delete, this is a command/keyword that deletes... - files in Windows cmd (command prompt) / batch. Another equivalent to this is the erase command, also in cmd / batch. - a keyword/function name in Python and various languages and software libraries, that deletes objects - also often used in other places to denote a deletion operation - a key on the keyboard

313 questions
2120
votes
19 answers

Delete an element from a dictionary

How do I delete an item from a dictionary in Python? Without modifying the original dictionary, how do I obtain another dict with the item removed? See also How can I remove a key from a Python dictionary? for the specific issue of removing an item…
richzilla
  • 40,440
  • 14
  • 56
  • 86
464
votes
22 answers

When is del useful in Python?

I can't really think of any reason why Python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
232
votes
8 answers

Which is better in python, del or delattr?

This may be silly, but it's been nagging the back of my brain for a while. Python gives us two built-in ways to delete attributes from objects, the del command word and the delattr built-in function. I prefer delattr because it I think its a bit…
pydanny
  • 7,954
  • 6
  • 34
  • 42
47
votes
8 answers

I don't understand this python __del__ behaviour

Can someone explain why the following code behaves the way it does: import types class Dummy(): def __init__(self, name): self.name = name def __del__(self): print "delete",self.name d1 = Dummy("d1") del d1 d1 = None print…
Brett Stottlemyer
  • 2,734
  • 4
  • 26
  • 38
45
votes
4 answers

Delete all objects in a list

I create many object then I store in a list. But I want to delete them after some time because I create news one and don't want my memory goes high (in my case, it jumps to 20 gigs of ram if I don't delete it). Here is a little code to illustrate…
Jean-Francois Gallant
  • 13,583
  • 6
  • 20
  • 24
43
votes
3 answers

`del` on a package has some kind of memory

del seems to have some memory which puzzles me. See the following: In [1]: import math In [2]: math.cos(0) Out[2]: 1.0 In [3]: del math.cos In [4]:…
Aguy
  • 7,851
  • 5
  • 31
  • 58
31
votes
4 answers

Python del statement

Calling del on a variable in Python. Does this free the allocated memory immediately or still waiting for garbage collector to collect? Like in java, explicitly calling del has no effect on when the memory will be freed.
totoromeow
  • 2,199
  • 4
  • 19
  • 19
30
votes
2 answers

In Python, is use of `del` statement a code smell?

I tend to use it whenever I am working on a prototype script, and: Use a somewhat common variable (such as fileCount), and Have a large method (20+ lines), and Do not use classes or namespaces yet. In this situation, in order to avoid potential…
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
20
votes
3 answers

How to delete every reference of an object in Python?

Supose you have something like: x = "something" b = x l = [b] How can you delete the object only having one reference, say x? del x won't do the trick; the object is still reachable from b, for example.
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
20
votes
9 answers

How do I delete the Nth list item from a list of lists (column delete)?

How do I delete a "column" from a list of lists? Given: L = [ ["a","b","C","d"], [ 1, 2, 3, 4 ], ["w","x","y","z"] ] I would like to delete "column" 2 to get: L = [ ["a","b","d"], [ 1, 2, 4 ], ["w","x","z"] …
P Moran
  • 1,624
  • 3
  • 18
  • 32
19
votes
3 answers

Node's del command - callback not firing

I'm working through a pluralsight course on gulp. John Papa is demonstrating how to inject a function that deletes existing css files, into the routine that compiles the new ones. The callback on the del function is not firing. The del function is…
Steve
  • 1,326
  • 14
  • 21
18
votes
5 answers

How to remove a word completely from a Word2Vec model in gensim?

Given a model, e.g. from gensim.models.word2vec import Word2Vec documents = ["Human machine interface for lab abc computer applications", "A survey of user opinion of computer system response time", "The EPS user interface management…
alvas
  • 115,346
  • 109
  • 446
  • 738
15
votes
4 answers

How does python close files that have been gc'ed?

I had always assumed that a file would leak if it was opened without being closed, but I just verified that if I enter the following lines of code, the file will close: >>> f = open('somefile.txt') >>> del f Just out of sheer curiosity, how does…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
15
votes
5 answers

Redis/Jedis - Delete by pattern?

Normally, I get the key set then use a look to delete each key/value pair. Is it possible to just delete all keys via pattern? ie: Del sample_pattern:*
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
13
votes
3 answers

Python del if in dictionary in one line

Is there a one line way of doing the below? myDict = {} if 'key' in myDic: del myDic['key'] thanks
scruffyDog
  • 721
  • 3
  • 7
  • 17
1
2 3
20 21