Using ‘+’ operator to add an element in the list in Python: The use of the ‘+’ operator causes Python to access each element of that first list. When ‘+’ is used a new list is created with space for one more element. Then all the elements from the old list must be copied to the new list and the new element is added at the end of this list.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- The ‘+’ operator refers to the accessor method and does not modify the original list.
- In this, sample_list doesn’t change itself. This type of addition of element in sample_list creates a new list from the elements in the two lists.
- The assignment of sample_list to this new list updates PythonList object so it now refers to the new list.
Complexity to add n elements
Have you wondered, how it works as the size of the Python List grows? Let us see with the explanation.
For every ith iteration, there will have to be i elements copied from the original list to form a new list. Considering the time taken to access an element from a list to be constant. So, the complexity or amount of time it takes to append n elements to the Python List i.e. sample_list we would have to add up all the list accesses and multiply by the amount of time it takes to access a list element plus the time it takes to store a list element. To count the total number of access and store operations we must start with the number of access and store operations for copying the list the first time an element is appended. That’s one element copied. The second append requires two copy operations. The third append requires three copy operations. So, we have the following number of list elements being copied.
Therefore, time complexity=O()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
It turns out that adding one more element to an already existing list is very efficient in Python. In fact, adding a new item to a list is an O(1) operation.
So overall complexity to append n elements is
Note: Proof that
.append() method has O(1) complexity to add new element is given by the accounting method to find the amortized complexity of append.
Graphical comparison between ‘+’ and ‘append’
- Difference between 'and' and '&' in Python
- Python set operations (union, intersection, difference and symmetric difference)
- Difference between == and is operator in Python
- Difference between Method and Function in Python
- Python | Difference between iterable and iterator
- Difference between List and Array in Python
- Python | Difference between Pandas.copy() and copying through variables
- Difference between List comprehension and Lambda in Python
- Difference between Python and C#
- Difference between C and Python
- Python | Difference Between List and Tuple
- Difference Between Go and Python Programming Language
- Difference between continue and pass statements in Python
- Difference between input() and raw_input() functions in Python
- Difference between dict.items() and dict.iteritems() in Python
- Python - Difference between sorted() and sort()
- Difference between List and Dictionary in Python
- Python: Difference between Lock and Rlock objects
- Python program to find difference between current time and given time
- Python - Difference between json.dump() and json.dumps()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.