What is the difference between working of append and + operator in a list in Python?

PythonProgrammingServer Side Programming

In this article, we will look at what is the difference between the append and the plus operator in a list.

  • The append() method is used to add elements to the list by utilizing a mutator() method.

  • The ‘+’ operator is used to create a new list with the capacity for one more element.

Behaviour of + operator while working with a list

Python accesses each element of the first list by using the '+' operator. When the '+' symbol is used, a new list with the capacity for one more element is produced. The old list's elements must then be copied to the new list, and the new element must be inserted at the end.

Example

In this example, we will see the usage of the + operator to add elements to a list in python.

list =[] n = 10 for i in range(n): list = list+[i] print(list)

Output

The output of the above code is as follow.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The complexity of adding an element to a list using the + operator

There will have to be ‘i’ elements copied from the original list for every iteration to construct a new list. Assuming that the time it takes to reach an element in a list is constant. So, to calculate the complexity or time it takes to append n entries to the Python List i.e. sample list, we would add up all the list accesses and multiply by the time it takes to access and save a list element.

To calculate the total number of access and store operations, start by counting the access and store operations for copying the list the first time an element is appended. That's one piece that's been cloned. Two copy operations are required for the second append. Three copy operations are required for the third append. So far, we've copied the following number of list elements. Therefore the time complexity is = O(n^2).

Behaviour of append() method when working with lists

Using the. append() technique, which is a time-saving method: The. append() method on lists tells the code to utilize a mutator method to add one more member to the list.

Example

This example explains how to use the append() method.

list =[] n = 10 for i in range(n): list.append(i) print(list)

Output

The output for the above code is as follows.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

It turns out that adding one additional entry to an already existing list is incredibly efficient in Python using the. append() method. Adding a new item to a list is an O(1) operation. As a result, the total complexity of appending n elements is O. (1).

The difference between the append and + operator

The ‘ +’ operator creates a new list in python when two lists are combined using it, the original object is not modified. On the other hand, using methods like extend and append, we add the lists in place, ie, the original object is modified. Also using append inserts the list as an object while ‘+’ just concatenates the two lists.

Example

The following example demonstrates the difference between the + operator and the append method in a list.

list1 = [1, 2, 3] list2 = ['a', 'b'] list3 = list1 + list2 print("Using + operator: ") print(list3) list1.append(list2) print("Using append method: ") print(list1)

Output

The output for the above code is as follows.

Using + operator:
[1, 2, 3, 'a', 'b']
Using append method:
[1, 2, 3, ['a', 'b']]
raja
Updated on 05-Sep-2022 08:34:00

Advertisements