Python list append time complexity

It's amortized O(1), not O(1).

Let's say the list reserved size is 8 elements and it doubles in size when space runs out. You want to push 50 elements.

The first 8 elements push in O(1). The nineth triggers reallocation and 8 copies, followed by an O(1) push. The next 7 push in O(1). The seventeenth triggers reallocation and 16 copies, followed by an O(1) push. The next 15 push in O(1). The thirty-third triggers reallocation and 32 copies, followed by an O(1) push. The next 31 push in O(1). This continues as the size of list is doubled again at pushing the 65th, 129th, 257th element, etc..

So all of the pushes have O(1) complexity, we had 64 copies at O(1), and 3 reallocations at O(n), with n = 8, 16, and 32. Note that this is a geometric series and asymptotically equals O(n) with n = the final size of the list. That means the whole operation of pushing n objects onto the list is O(n). If we amortize that per element, it's O(n)/n = O(1).

Learn about append in Python.

Overview

The append() function in python helps us add an element at the end of the list. It doesn't create a new list but modifies the existing one.

Syntax of append() Function in Python

Append function in python has the following syntax:

The myList is the list where we want to add the element.

Parameters of append() Function in Python

It accepts a single element which could be any Python object

  • integer, floating number, boolean, string, list, user-defined object etc.

This parameter is mandatory and can't be omitted. Leaving this parameter empty will give an error.

Return Value of append() Function in Python

Return Type: None

The append function doesn't return anything. It only modifies the original list.

Example of append() Function in Python

Here we will add elements like integers, floating numbers, and a string to a list.

list1 = ['programming', 5, 'Hello geeks']

list1.append(10)

list1.append('python')

list1.append(27.5)

# Updated list after adding new elements
print(list1)

Output

['programming', 5, 'Hello geeks', 10, 'python', 27.5]

Explanation

  • We created an original list that has some initial values in it.
  • We then added an integer value at the end of the list.
  • After that, we added a string followed by a floating value.
  • Finally, we printed the original list and saw that all the new items get added to the list at the end in the same order.

What is append() Function in Python?

For example, if you have a list of overall toppers of the school, this list will always need to be updated every year to add new toppers at the end of the list. Ever wondered how you would update the list most easily? The answer to this problem is the python append function, which adds an element at the end of the list.

The append() function of python is used to add an item at the end of the list on which it is applied. The append function takes an element as a parameter to be added to the list.

The append function doesn't create a new list after adding the item, but it modifies the original list, i.e., it updates the same list by adding the item at its end.

Time Complexity for Append in Python

This function has constant time complexity i.e. O(1), because lists are randomly accessed so the last element can be reached in O(1) time that's why the time taken to add the new element at the end of the list is O(1).

Also, when a list is created in python, it reserves 32 bits of the contiguous memory location. Whenever the combined size of elements of that list exceeds the memory space of the original list, it acquires the contiguous memory at another location of size double its previous size. In that scenario, the previous elements are copied to this new memory space, and new elements are appended to the list. So, this is considered as the worst case for appending any element to the list and the time complexity for this worst case is O(N) where N is the size of the original list.

More Examples

Example 1: Adding iterables to the existing list

Here, we will add iterables like lists, sets, or a dictionary to an existing list.

list1 = ['programming', 5, 'Hello geeks']

list1.append(['python', 'javascript', 50])

list1.append({50,40,10,10,20})

list1.append({"key1":"Value1", "key2":"Value2"})

print(list1)

Output

['programming', 5, 'Hello geeks', ['python', 'javascript', 50], {40, 50, 10, 20}, {'key1': 'Value1', 'key2': 'Value2'}]

Explanation

  • We created an original list that has some initial values in it.
  • We then added a list with different values at the end of the original list.
  • After that, we added a set followed by a dictionary to the list.
  • Finally, we printed the original list, and we saw that all the new items get added to the list at the end in the same order.

Example 2: Nested Lists

Here we are going to append a list2 into a list1 and then another list3 into the list2, let's see what will happen.

list1 = ['1','2','3']  


list2 = ['4','5','6','7']  
list1.append(list2)  

# Displaying the original list
print(list1)   

# Nested list  
list2.append(['8','9','10'])

# Displaying the original list
print(list1)   

Output:

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

Explanation

  • We created an original list1 which has some initial values in it.
  • We then added a list2 with different values in it at the end of the original list1.
  • After that, we added another list3 to the list2. As the list holds the address of the element, the changes will also appear in the list1.
  • Finally, we printed the original list, and we saw that all the new items get added to the list2 and hence in the list1 at the end in the same order.

Conclusion

  • We talked about what's the append function in python, then we saw how the append function is used in python. After that, we explored the function in detail, along with examples.
  • Here are a few takeaways from the entire discussion.
    • The append function doesn't modify the original list.
    • It appends the element provided as a parameter to the end of the list.
    • There is no Return Value.

Is appending to a list Slow?

append occasionally will have to copy all the elements of a list to a bigger list, . append must be slower than pre-allocating the entire length of the list and assigning to individual indices of that list (of course you can only do that if you know how big you will need the list to be beforehand).

What is the worst

Golang: The time complexity of append() The short answer: The amortized cost of append() is O(1), but the worst-case cost is O(N).

What is the time complexity of pop from a list in Python?

Yes, it is O(1) to pop the last element of a Python list, and O(N) to pop an arbitrary element (since the whole rest of the list has to be shifted). So just to make it clear, list. pop(0) is O(n) and list. pop() is O(1).

What is the worst

The short answer is that when starting from an empty dynamic array and adding N elements, the total time is O(N). You are correct that adding a single item has the worst-case time of O(N) when a resize must be performed, and that O(log N) resizes take place.