How to append objects in a list in Python?


List is one of the most commonly used data structures provided by python. List is a data structure in Python that is mutable and has an ordered sequence of elements. Following is a list of integer values

Example

Following is a list of integer values.

lis= [11,22,33,44,55] print(lis)

Output

If you execute the above snippet, it produces the following output.

[11, 22, 33, 44, 55]

In this article, we will discuss how to add objects to a list and the different ways to add objects such as append(), insert(), and extend() methods in Python.

Using append() method

In this method, we use append() to add objects to a list. The append() method adds a new element to the end of a list that already exists.

Syntax

The syntax of the append() method is as follows.

list_name.append(element)

Where,

  • list.name is the name of the list.

  • append() is the list method for adding an item to the end of the list_name.

  • element is an element or an individual item that you want to add.

Example 1

In this example, we used the append() method to add objects to a list. Here, we have added another name to the list of names(names_list).

names_list = ["Meredith", "Levi", "Wright", "Franklin"] names_list.append("Kristen") print(names_list)

Output

The output of the above code is as follows.

['Meredith', 'Levi', 'Wright', 'Franklin', 'Kristen']

Example 2

Following is another example to append the element in a list −

numbers_list = [2, 5, 46, 78, 45] numbers_list.append(54) print ('The list with the number appended is:',numbers_list)

Output

The list with the number appended is: [2, 5, 46, 78, 45, 54]

Using the insert() method

In this method, we use insert() to add objects to a list. The insert() method adds a new element at the specified position of the list.

Syntax

The syntax of the insert() method is as follows.

list_name.insert(pos,ele)

Where,

  • list.name is the name of the list.

  • insert() is a list method to insert element at a specified position.

  • pos is an integer where it specifies the position or the index of the element you want to add.

  • ele is the element that needs to be added.

Example

In this example, to add an item at the 2nd position of the list we used the insert() method.

lst = ["Bat", "Ball"] lst.insert(2,"Wicket") print(lst)

Output

The output of the above code is as follows.

['Bat', 'Ball', 'Wicket']

Using the extend() method

In this method, we will look at extend() method to combine all elements from one list into another by concatenating (adding) them together.

Syntax

The syntax of the extend() method is as follows.

list_name.extend(other_list/iterable)

Where,

  • list_name is the name of one of the lists.

  • extend() is the method for adding all contents of one list to another.

  • iterable can be any iterable such as another list, for example, other_list. In that case, other_list is a list that will be concatenated with list_name, and its contents will be added one by one to the end of list_name, as separate items.

Example

In the following code, we will concatenate two lists using the extend() method.

names_list = ["Meredith", "Levi"] othernames_list = [ "Franklin", "Wright"] names_list.extend(othernames_list) print(names_list)

Output

The output of the above code is as follows.

['Meredith', 'Levi', 'Franklin', 'Wright']

Updated on: 03-Nov-2023

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements