del() vs del statement in python [duplicate]










4
















This question already has an answer here:



  • Why does del (x) with parentheses around the variable name work?

    1 answer



  • python assert with and without parenthesis

    5 answers



>>> 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










share|improve this question















marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 5





    You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

    – khelwood
    Nov 15 '18 at 15:32












  • okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

    – Srinivas
    Nov 15 '18 at 15:34






  • 3





    Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

    – Gerges
    Nov 15 '18 at 15:34











  • You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

    – Chris_Rands
    Nov 15 '18 at 15:35






  • 2





    You can't write a del function in your module. del is a keyword.

    – khelwood
    Nov 15 '18 at 15:37
















4
















This question already has an answer here:



  • Why does del (x) with parentheses around the variable name work?

    1 answer



  • python assert with and without parenthesis

    5 answers



>>> 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










share|improve this question















marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 5





    You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

    – khelwood
    Nov 15 '18 at 15:32












  • okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

    – Srinivas
    Nov 15 '18 at 15:34






  • 3





    Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

    – Gerges
    Nov 15 '18 at 15:34











  • You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

    – Chris_Rands
    Nov 15 '18 at 15:35






  • 2





    You can't write a del function in your module. del is a keyword.

    – khelwood
    Nov 15 '18 at 15:37














4












4








4


2







This question already has an answer here:



  • Why does del (x) with parentheses around the variable name work?

    1 answer



  • python assert with and without parenthesis

    5 answers



>>> 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










share|improve this question

















This question already has an answer here:



  • Why does del (x) with parentheses around the variable name work?

    1 answer



  • python assert with and without parenthesis

    5 answers



>>> 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





This question already has an answer here:



  • Why does del (x) with parentheses around the variable name work?

    1 answer



  • python assert with and without parenthesis

    5 answers







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:50









dawg

59.9k1285154




59.9k1285154










asked Nov 15 '18 at 15:29









SrinivasSrinivas

1299




1299




marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Chris_Rands python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 15:45


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 5





    You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

    – khelwood
    Nov 15 '18 at 15:32












  • okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

    – Srinivas
    Nov 15 '18 at 15:34






  • 3





    Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

    – Gerges
    Nov 15 '18 at 15:34











  • You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

    – Chris_Rands
    Nov 15 '18 at 15:35






  • 2





    You can't write a del function in your module. del is a keyword.

    – khelwood
    Nov 15 '18 at 15:37













  • 5





    You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

    – khelwood
    Nov 15 '18 at 15:32












  • okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

    – Srinivas
    Nov 15 '18 at 15:34






  • 3





    Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

    – Gerges
    Nov 15 '18 at 15:34











  • You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

    – Chris_Rands
    Nov 15 '18 at 15:35






  • 2





    You can't write a del function in your module. del is a keyword.

    – khelwood
    Nov 15 '18 at 15:37








5




5





You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

– khelwood
Nov 15 '18 at 15:32






You can put parentheses in lots of places without affecting anything. Here they're simply redundant. Just like return 1 is the same as return (1).

– khelwood
Nov 15 '18 at 15:32














okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

– Srinivas
Nov 15 '18 at 15:34





okay, if i write del method in my module and use the above at same time, how python interpreter differentiates between the, although i never tried until now.

– Srinivas
Nov 15 '18 at 15:34




3




3





Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

– Gerges
Nov 15 '18 at 15:34





Case 2 is not a function call, it is still a statement call. The same reason why if(cond) and if (cond) and if cond are all the same.

– Gerges
Nov 15 '18 at 15:34













You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

– Chris_Rands
Nov 15 '18 at 15:35





You should stick with del ((((((((((((((((((((( list[0] ))))))))))))))))))))) ;)

– Chris_Rands
Nov 15 '18 at 15:35




2




2





You can't write a del function in your module. del is a keyword.

– khelwood
Nov 15 '18 at 15:37






You can't write a del function in your module. del is a keyword.

– khelwood
Nov 15 '18 at 15:37













2 Answers
2






active

oldest

votes


















9














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.






share|improve this answer
































    0














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



    it's exactly the same thing






    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      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.






      share|improve this answer





























        9














        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.






        share|improve this answer



























          9












          9








          9







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 15:38

























          answered Nov 15 '18 at 15:32









          NetwaveNetwave

          13.5k22246




          13.5k22246























              0














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



              it's exactly the same thing






              share|improve this answer



























                0














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



                it's exactly the same thing






                share|improve this answer

























                  0












                  0








                  0







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



                  it's exactly the same thing






                  share|improve this answer













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



                  it's exactly the same thing







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 15:33









                  iEldeniElden

                  692618




                  692618













                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3