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

LISTS, TUPLES, SETS AND DICTIONARY · Part 5

Chapter 3: 4 · COMPUTER SCIENCE

to an existing list. Syntax: List.append (element to be added) List.extend ( [elements to be added]) In extend() function, multiple elements should be specified within square bracket as arguments of the function. >>> Mylist=[ , , ] >>> Mylist.append( ) >>> print(Mylist) [ , , , ] Example In the above example, Mylist is created with three elements. Through >>> Mylist.append( ) statement, an additional value is included with the existing list as last element, following print statement shows all the elements within the list MyList.

>>> Mylist.extend([ , , ]) >>> print(Mylist) [ , , , , , , ] Example In the above code, extend() function is used to include multiple elements, the print statement shows all the elements of the list after the inclusion of additional elements. . . Inserting elements in a list As you learnt already, append() function in Python is used to add more elements in a list.

But, it includes elements at the end of a list. If you want to include an element at your desired position, you can use insert () function. The insert() function is used to insert an element at any position of a list. Syntax: List.insert (position index, element) >>> MyList=[ , , ,'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan' ] >>> print(MyList) [ , , , 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan'] >>> MyList.insert( , 'Ramakrishnan') >>> print(MyList) [ , , , 'Ramakrishnan', 'Kannan', 'Gowrisankar', 'Lenin', 'Sreenivasan'] Example In the above example, insert() function inserts a new element ‘Ramakrishnan’ at the index value , ie.

at the th position. While inserting a new element in between the existing elements, at a particular location, the existing elements shifts one position to the right. 12th Computer Chapter - - . .

Deleting elements from a list There are two ways to delete an element from a list viz. del statement and remove() function. del statement is used to delete elements whose index is known whereas remove() function is used to delete elements of a list if its index is unknown. The del statement can also be used to delete entire list.

Syntax: del List [index of an element] # to delete a

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 →