📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 138question

LISTS, TUPLES, SETS AND DICTIONARY · Part 6

Chapter 3: 4 · COMPUTER SCIENCE

particular element del List [index from : index to] # to delete multiple elements del List # to delete entire list >>> MySubjects = ['Tamil', 'Hindi', 'Telugu', 'Maths'] >>> print (MySubjects) ['Tamil', 'Hindi', 'Telugu', 'Maths'] >>> del MySubjects[ ] >>> print (MySubjects) ['Tamil', 'Telugu', 'Maths'] Example In the above example, the list MySubjects has been created with four elements. print statement shows all the elements of the list. In >>> del MySubjects[ ] statement, deletes an element whose index value is and the following print shows the remaining elements of the list. >>> del MySubjects[ : ] >>> print(MySubjects) ['Tamil'] Example In the above codes, >>> del MySubjects[ : ] deletes the second and third elements from the list.

The upper limit of index is specified within square brackets, will be taken as - by the python. 12th Computer Chapter - - Lists, Tuples, Sets and Dictionary >>> del MySubjects >>> print(MySubjects) Traceback (most recent call last): File "<pyshell# >", line , in <module> print(MySubjects) NameError: name 'MySubjects' is not defined Example Here, >>> del MySubjects, deletes the list MySubjects entirely. When you try to print the elements, Python shows an error as the list is not defined. Which means, the list MySubjects has been completely deleted.

As already stated, the remove() function can also be used to delete one or more elements if the index value is not known. Apart from remove() function, pop() function can also be used to delete an element using the given index value. pop() function deletes and returns the last element of a list if the index is not given. The function clear() is used to delete all the elements in list, it deletes only the elements and retains the list.

Remember that, the del statement deletes entire list. Syntax: List.remove(element) # to delete a particular element List.pop(index of an element) List.clear( ) >>> MyList=[ , , ,'Kannan', 'Gowrisankar', 'Lenin'] >>> print(MyList) [ , , , 'Kannan', 'Gowrisankar', 'Lenin'] >>> MyList.remove( ) >>> print(MyList) [ , , 'Kannan', 'Gowrisankar', 'Lenin'] Example In the above example, MyList has been created with three integer and three string elements, the following print statement shows all

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →