Short answer
Big O in Python means what it means anywhere: how the time an operation takes grows as its input grows. What is specific to Python is the cost of its built-in types, and knowing those costs helps you choose the right one for a job.
Lists
A Python list is a dynamic array, not a linked list. Indexing and appending at the end are cheap, while operations at the front or in the middle move many items:
items = []
items.append(42) # O(1) amortized: usually cheap, occasionally resizes
items.pop() # O(1): removes the last item
items.insert(0, 7) # O(n): every existing item shifts one slot right
items.pop(0) # O(n): every remaining item shifts one slot left
“Amortized” here means that an occasional resize copies the whole list, but averaged over many appends, the cost per append stays constant.
Membership tests
The in operator behaves very differently on a list than on a set or a dict:
target in my_list # O(n): may check every item
target in my_set # O(1) on average
key in my_dict # O(1) on average
The set and dict costs are averages. They assume the hash function spreads keys well. In the worst case, when many keys collide, a lookup can take O(n) time. A membership test on a list inside a loop over n items makes the whole job O(n²), which is a common source of slow Python code.
Sorting
list.sort() and sorted() run in O(n log n) time in the worst case. The Python documentation notes that already sorted or reverse-sorted input takes only about O(n) comparisons, because the sort is adaptive. CPython, the standard Python implementation, uses Timsort for this.
Deques
For a queue, or any work that adds and removes items at both ends, collections.deque is the usual choice:
from collections import deque
queue = deque()
queue.append("first") # O(1)
queue.popleft() # O(1); list.pop(0) would be O(n)
Indexing into the middle of a deque is O(n), so use a list when you need fast access by position.
Other common costs
len()is O(1), because Python stores the number of items rather than counting them each time.- Inserting and removing dict and set entries are O(1) on average.
- Building a list of n items with a loop costs O(n) in total, since each append is amortized O(1).
The general version of these costs, separate from any language, is covered in Big O for Data Structures.
Reading Big O in Python code
Look for a loop that scans a collection while another search runs inside it. Nested work over the same collection often means O(n²). In Python, a membership test on a list inside a loop is the most common case.
Is DSA better in C++ or Python?
People asking “Is DSA better in C++ or Python?” are usually choosing a language for learning or interviews. The complexity ideas are the same in both. The practical differences are how much the language gives you by default, such as Python’s built-in lists and dicts, and how much control you get over memory layout in C++. Big O does not settle that choice, but it helps you judge the operations once you have picked a language.
Constants and measuring
Big O leaves out constant factors and interpreter overhead, so it describes growth rather than predicting the winner at small sizes. A list membership test on a handful of items can be faster in practice than a set lookup, because hashing has its own overhead. To measure real speed on your own machine, the standard library’s timeit module is the usual tool.