image
image
image

Deleting Multiple Elements

image

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

del list_mine[0:3]

Example

print(list_mine)  #a, m

Delete Entire List

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

delete list_mine

print(list_mine)  #will generate an error of lost not found 

The remove() method or pop() function may be used to remove specified item. The pop() method will remove and return the last item if index is not given and helps implement lists as stacks. The clear() method is used to empty a list.

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

list_mine=[‘t’,’k’,’b’,’d’,’w’,’q’,’v’]

list_mine.remove(‘t’)

print(list_mine)    #output will be [‘t’,’k’,’b’,’d’,’w’,’q’,’v’]

print(list_mine.pop(1))   #output will be ‘k’

print(list_mine.pop())   #output will be ‘v’

Practice Exercise

Given list_yours=[‘K’,’N’,’O’,’C’,’K’,’E’,’D’]

a. Pop the third item in the list, save the program as list1.

b. Remove the fourth item using remove() method and save the program as list2

c. Delete the second item in the list and save the program as list3.

d. Pop the list without specifying an index and save the program as list4.