In this article we will discuss how to add element in an existing list using different techniques.

Adding item in list using list.append()

It adds the item at the end of list.

For example, we have a list of string i.e.

Now let’s add an element at the end of this list using append() i.e.

Now list will become,

Passing an another list as a parameter in list.append()

As list can contain different kind of elements, so if we pass an another list object as parameter in append() i.e.

Then whole list object will be added to the end of list. So, list contents will be now,

Adding all elements of one list to another using list.extend()

It will add all elements of list1 at the end of list. Basically it will merge the two lists i.e.

Now list contents will be,

list append() vs extend()

list.append(item) , considers the parameter item as an individual object and add that object in the end of list. Even if given item is an another list, still it will be added to the end of list as individual object i.e.

List will become,

list.extend(item) , considers parameter item to be an another list and add all the individual elements of the list to the existing list i.e.

List will become,

Complete example is as follows,

Output

 

Click Here to Subscribe for more Articles / Tutorials like this.