Deleting list elements<\/strong><\/p>\nTo remove a list element, one can use either the del statement (if you know the element index to delete) or remove () method (if you do not know the element index, but the element itself, discussed in section 4.1.9). The following example depicts the deletion of an element using the del statement.<\/p>\n
>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> del a [ 1 ]\r\n>>> a\r\n[ ' spam ' , 100 , 1234 ]<\/pre>\nThe del statement can also be used to explicitly remove the entire list.<\/p>\n
>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> del a \r\n>>> a\r\nTraceback ( most recent call last ) :\r\nFile \" <stdin> \" , line 1 , in <module>\r\nNameError: name ' a ' is not defined<\/pre>\nThe following is an interesting case of the del statement:<\/p>\n
>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> del a [ 1 ] , a [ 1 ]\r\n>>> a\r\n[ ' spam ' , 1234 ]<\/pre>\nIt should be noted that the same index is deleted twice. As soon as an element is deleted, the indices of succeeding elements are changed. So deleting an index element n times, would actually delete n elements.<\/p>\n
It is also possible to delete multiple items from a list.<\/p>\n
>>> a= [ ' spam ' , ' eggs ' , 100 , 1234 ]\r\n>>> a [ 1 : 3 ] = [ ]\r\n>>> a\r\n[ ' spam ' , 1234 ]<\/pre>\nSwapping lists<\/strong><\/p>\nThere might be a scenario in which multiple lists need to be swapped among themselves. This is can done easily using multiple assignments expression.<\/p>\n
>>> a= [ 10 , 20 , 30 ] \r\n>>> b= [ 40 , 50 , 60 ] \r\n>>> c= [ 70 , 80 , 90 ]\r\n>>> a , b , c=c , a , b \r\n>>> a\r\n[ 70 , 80 , 90 ] \r\n>>> b\r\n[ 10 , 20 , 30 ]\r\n>>> c\r\n[ 40 , 50 , 60 ]<\/pre>\n","protected":false},"excerpt":{"rendered":"In this Page, We are Providing Python Programming \u2013 Deleting List Elements. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Deleting List Elements Deleting list elements To remove a list element, one can use either the del statement (if you know the element index to delete) or remove …<\/p>\n
Python Programming \u2013 Deleting List Elements<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Deleting List Elements - Python Programs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n