Each time you remove an element from the list, the elements "shift down" in position. This takes time, as you are essentially recreating (large portions of) the list after each removal. When you create a new list, you build it once. A more Pythonic way to do this is to use a list comprehension. clw = [word for word in words if len(word) == length] Answer from mopslik on reddit.com
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ TimeComplexity
TimeComplexity - Python Wiki
[3] = For these operations, the worst case n is the maximum size the container ever achieved, rather than just the current size. For example, if N objects are added to a dictionary, then N-1 are deleted, the dictionary will still be sized for N objects (at least) until another insertion is made.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 49732932 โ€บ python-list-del-insert-no-of-assignments-and-time-complexity
Python list - del, insert, no. of assignments and time complexity - Stack Overflow
I'm wondering about how many assignments python makes during an insert on an arbitrary index and how many assignments is being made when deleting the first element del(list[0]). I'm a little bit confused since the time complexity for these 2 operations are O(n), n being the number of elements in the list, but when thinking about it I get it to be O(n-1).
Discussions

Why is removing elements from a list so slow, and is there a faster way?
Each time you remove an element from the list, the elements "shift down" in position. This takes time, as you are essentially recreating (large portions of) the list after each removal. When you create a new list, you build it once. A more Pythonic way to do this is to use a list comprehension. clw = [word for word in words if len(word) == length] More on reddit.com
๐ŸŒ r/learnpython
25
6
April 21, 2024
how much time will it take to remove a key, value pair from a dictionary?
Fixed time (O(1)). If you need the exact value on your computer, run a benchmark. More on reddit.com
๐ŸŒ r/learnpython
9
6
June 23, 2023
How to delete from a deque in constant time without "pointers"?

There's a technique which I call 'lazy popping' which can help here.

The idea is that you don't delete immediately from the queue. Rather, you leave deleted items in the queue, but mark them as deleted in another data structure -- usually a set. Whenever you have to pop an item to execute, keep popping until you reach an item that hasn't yet been deleted.

This gives you constant-time push, amortized constant-time pop (although you may pop multiple deleted items off the queue each time you pop an item to execute, each item only gets popped exactly once) , and constant-time deletion, which is better than what you can get by maintaining a list and deleting from start or middle.

In this case, you'd save the IDs of deleted items in the set. It looks like this (untested code):

import collections

class DeletableQueue:
    def __init__(self):
        self.deleted = set()
        self.queue = collections.deque()
    def push(self, item):
        self.queue.append(item)
    def pop(self):
        # Precondition: there is at least one non-deleted item on the queue.
        while id(q[0]) in deleted:
            q[0].pop_left()  # Discard an already-deleted item.
        return q.pop_left()  # Return the actual item to pop
    def delete(self, item_to_delete):
        self.deleted.add(id(item_to_delete))
More on reddit.com
๐ŸŒ r/learnpython
15
6
October 20, 2014
Time Complexity for Dictionary Python
For dictionaries/HashTables the Average is O(1), worst case is still O(n) More on reddit.com
๐ŸŒ r/leetcode
5
5
September 29, 2022
๐ŸŒ
Finxter
blog.finxter.com โ€บ what-is-the-difference-between-remove-pop-and-del-in-lists-in-python
What is The Difference Between remove(), pop() and del in Lists in Python? โ€“ Be on the Right Side of Change
November 1, 2021 - If you specify an element with the wrong index, Python raises an IndexError. li = [3, 5, 7, 2, 6, 4, 8] del li[8] print(li) # IndexError: list assignment index out of range ยท The computational complexity when removing an element in index i from the list of n elements using del is O(n-i). Case ...
๐ŸŒ
GitHub
gist.github.com โ€บ Gr1N โ€บ 60b346b5e91babb5efac
Complexity of Python Operations ยท GitHub
Complexity of Python Operations. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
DEV Community
dev.to โ€บ williams-37 โ€บ understanding-time-complexity-in-python-functions-5ehi
Understanding Time Complexity in Python Functions - DEV Community
October 25, 2024 - Pythonโ€™s built-in sorting algorithm (Timsort) has a time complexity of O(n log n) in the average and worst cases. ... Retrieving a value by key in a dictionary is a constant time operation due to the underlying hash table implementation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ complexity-cheat-sheet-for-python-operations
Complexity Cheat Sheet for Python Operations - GeeksforGeeks
December 13, 2024 - Pythonโ€™s set is another hash-based collection, optimized for membership checks and set operations: Tuples are immutable sequences, making them lighter but with limited operations compared to lists: Strings are immutable and behave similarly to tuples in terms of time complexities: ... Amortized complexity applies to append() because resizing happens occasionally. Insertions and deletions at arbitrary positions require shifting elements.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ why is removing elements from a list so slow, and is there a faster way?
r/learnpython on Reddit: Why is removing elements from a list so slow, and is there a faster way?
April 21, 2024 -

I was trying to write a simple application, which is ao supposed to filter a list of words down to a list of words of a certain length. For that I could either remove the words of the wrong length, or create a new list of words with the correct length.

I had a list of around 58000 words, and wanted to filter out all the 6 letter words, which are around 6900.

with open('words.txt') as f:
    words = f.readlines()
    for i in range(len(words)):
        words[i] = words[i].strip()

length = int(input("Desired word length "))

for i in reversed(words):
    if len(i) != length:
        words.remove(i)

This took 22 seconds.

Another way is to just create a new list with words of the correct length. I did this as follows:

with open('words.txt') as f:
    words = f.readlines()
    for i in range(len(words)):
        words[i] = words[i].strip()

length = int(input("Desired word length "))
clw = []

for i in words:
    if len(i) == length:
        clw.append(i)

This only took 0.03 seconds. How can it be that creating a list of 6900 words takes 0.03 seconds, but removing 51100 words takes 22? It's only 7 times as many words, but takes 700 times as long. And is there a better and faster way to quickly remove list elements?

๐ŸŒ
UCI
ics.uci.edu โ€บ ~pattis โ€บ ICS-33 โ€บ lectures โ€บ complexitypython.txt
Complexity of Python Operations
In fact, we could also simplify copy = list(alist) O(N) copy.sort() O(N Log N) - for fast Python sorting to just copy = sorted(alist) O(N Log N) - for fast Python sorting because sorted will create a list of all the values in its iterable argument, and return it after mutating (sorting) it.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ global_codess โ€บ time-complexities-of-python-data-structures-3bja
Time Complexities Of Python Data Structures - DEV Community
February 19, 2020 - For example, a linear searchโ€™s worst case occurs when x is not present in the list so the iteration compares x with all the elements.This would give us a run-time of O(n) <h5>Time Complexities of Python Data structures</h5> 1. List Insert: ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexities-of-python-dictionary
Time Complexities of Python Dictionary - GeeksforGeeks
July 23, 2025 - Removing an element by its key also has an average time complexity of O(1) using the same hash table mechanism. Python ยท dict = {'a': 1, 'b': 2, 'c': 3} # Remove a key-value pair del dict['b'] print(dict) The 'in' keyword allows us to check ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-what-is-the-time-complexity-of-list-append-and-remove-operations-in-python-397728
What is the time complexity of list append and remove operations in Python | LabEx
This is because the list.append() operation simply adds a new element to the end of the list, and the underlying implementation of the Python list data structure is designed to handle this operation efficiently. Here's an example code snippet to demonstrate the constant time complexity of the list.append() operation:
๐ŸŒ
Pythontutor
pythontutor.net โ€บ home โ€บ python tutorial โ€บ python dictionaries โ€บ dictionary time complexity
Python Dictionary Time Complexity โ€“ Operations Explained with Examples
Deleting a dictionary entry also runs in constant time in most cases. ... Python quickly determines whether a key exists using hashing. Iterating through a dictionary requires visiting every element, so the time complexity grows linearly with the size of the dictionary.
๐ŸŒ
Quora
quora.com โ€บ Why-is-the-time-complexity-of-deleting-an-item-from-an-array-linear-O-n-and-not-constant
Why is the time complexity of deleting an item from an array linear - O(n) and not constant? - Quora
This is simply not true: the complexity may be constant depending on your assumptions. What are you planning to accomplish? What is allowed and what is not? What is purposeful and what is accidental? For instance, deleting the last item is most certainly [math]\,O(1)\,[/math] however you choose to do it: maybe you just decrement the length of the array, or you replace the value with a sentinel, etc.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ recursion vs dynamic programming โ€“ fibonacci(leetcode 509)
Recursion vs Dynamic Programming - Fibonacci(Leetcode 509) | Towards Data Science
March 5, 2025 - And the space complexity would be O(N) since we need to store all intermediate values into our dp_list. So the space we need is the same as n given. ... The red line represents the time complexity of recursion, and the blue line represents dynamic programming.
๐ŸŒ
Bradfield CS
bradfieldcs.com โ€บ algos โ€บ analysis โ€บ performance-of-python-types
Performance of Python Types
Python is still an evolving language, which means that the above tables could be subject to change. The latest information on the performance of Python data types can be found on the Python website. As of this writing, the Python wiki has a nice time complexity page that can be found at the ...
๐ŸŒ
Quora
quora.com โ€บ What-are-the-time-complexity-considerations-of-lists-in-Python
What are the time complexity considerations of lists in Python? - Quora
Answer: In a normal list on average: ... Slice : O(k) * Sort : O(n log n) - n is the length of the list * Len : O(1) * Pop : O(1) - pop from end * Insert : O(n) - n is the length of the list * Del : O(n) - n......