9
>>> li = [1, 2, 3, 4]
>>> li
[1, 2, 3, 4]
>>> del li[2] #case 1
>>> li
[1, 2, 4]
>>> del(li[2])  # case 2
>>> li
[1, 2]
>>> del (li[1]) # case 3
>>> li
[1]
>>>

One of my professors used case 2 to delete item from list.
As per python documentation case 1 is right and there is also another syntactic way exist from this answer so case 3 also right, but as per my knowledge there is no del method exist in python, how case 2 is valid. I searched whole python documentation but could not find it.

Update: if i write del method myself in my module and use case 2 at same time, how python interpreter differentiates between them or will it through an error, although i never tried until now

dawg
  • 98,345
  • 23
  • 131
  • 206
Srinivas
  • 464
  • 3
  • 17

2 Answers2

11

All of them are the same, del is a keyword as yield or return, and (list[1]) evaluates to list[1]. So del(list[1]) and del (list[1]) are the same. For the base case, since you dont have the () you need to force the extra space, hence del list[1].

EDIT: You cannot redifine del since it is a language keyword.

Netwave
  • 40,134
  • 6
  • 50
  • 93
1

The parenthehis is not mandatory with keyword (like if or del), but can put some if you want.

it's exactly the same thing

iElden
  • 1,272
  • 1
  • 13
  • 26