
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Append and Plus Operator in Python List
In this article, we will see the differences between two common ways of adding elements to lists in Python: the append method and the "+" operator.
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 "+" with Python Lists
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
When you run the program, it will show this output -
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
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() 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
After running the program, you will get this result -
[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).
Difference Between 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
This output will be displayed when the program runs:
Using + operator: [1, 2, 3, 'a', 'b'] Using append method: [1, 2, 3, ['a', 'b']]