Built in functions of List

Betaspyder
3 min readAug 25, 2022

--

In this article, We will understand some built in functions, which will be used in list.

from internet
  • append()
  • extend()
  • insert()
  • remove()
  • pop()
  • clear()
  • Bonus : del statement

append():

Append function is used to add only one element at the end of the list. Element cannot be added in between the list.

appending a value
Appending error

Look at the above image, I have give 2 values in the append function. It shows an error that “takes exactly one arguments”.

extend():

Extend function is used to add multiple elements in the existing list. However, it adds the element only at the end of the list. let’s take an example.

extend() function

insert():

Insert function is used to add the element at the required position. insert function takes 2 arguments. 1st argument is the index position where we want to insert the element and the second argument is the element which we want to insert.

insert() function

In the 2nd index, string Welcome has been added. Remember that the starting index is zero.

remove():

remove function is used to remove the element by giving the element which we want to remove.

remove function

The above example shows that the element 23 has been removed from the list.

pop():

Pop function is used to remove the element by giving its index position.

pop function

in the above image, pop() removed the element of the index position 1. the element in the first position is 85 and it has been removed.

so, if we want to remove the element by using index, pop() function should be used.

clear():

clear function clears the element from the list. But the structure of the list will remain. let’s understand by an example.

clear function

All the elements from the list has been removed. only the empty list remains as output.

We have come to an end of the article. The last which we are going to see is that del statement.

del:

del statement is used to delete the entire list.

del statement

del is a statement. it should not be used as function like MyList.del()

it should be written as del MyList. In the above example, we have printed the list, then we have deleted the list by using del statement, again we have printed using print. It shows the error that the list is not defined. Yes, the list has been deleted. The deleted list cannot be printed and thus it shows the error.

If you like this article please give a clap and follow me on Twitter.

if you want to access full coding of this article, click here . Happy coding…!!! and stay tuned…

--

--

Betaspyder

Technical Writer, Content Creator, Content Writer, Python, Data Analyst, Database, HTML, Javascript