0

Suppose a dictionary is:

sd = {'q':1,'w':2,'e':4}

i know we can delete a pair though deleting respective key? but can we just delete a value? i got this question in a interview thats why i asked here.

Chidananda Nayak
  • 175
  • 1
  • 1
  • 11
  • 1
    You can delete by `del sd['q']` so that `q` and its respective value gets deleted –  May 22 '18 at 16:29
  • 2
    Possible duplicate of [Delete an element from a dictionary](https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary) – William_Wilson May 22 '18 at 16:30
  • @kvmahesh well i know we can delete a pair though deleting respective key? but can we just delete a value? i got this question in a interview thats why i asked here. – Chidananda Nayak May 22 '18 at 16:30
  • 4
    Answer would be 'No, you can not delete a value from a dictionary' – python_enthusiast May 22 '18 at 16:32
  • 1
    No, the best you can do is to replace the value, eg with `None`, or some default value. Eg, `default = object(); sd['q'] = default` – PM 2Ring May 22 '18 at 16:51

1 Answers1

0

In short, No. You can't delete a value in a key-value pair but you can set the value to be None.

ramazan polat
  • 7,111
  • 1
  • 48
  • 76