POST 1 of 5 MorningDSAConcept
Python list = dynamic array
Most Python developers use list every day without thinking about what it actually is. Worth thinking about. A Python list is a dynamic array. Memory-contiguous. When you create [1, 2, 3], CPython allocates a small contiguous block, stores pointers to the three integer objects in it, and tracks length and capacity. When you append and exceed capacity, Python allocates a new block (typically 1.125x the old size — Python's growth factor is small, not 2x), copies pointers over, and frees the old block. Why this matters for your daily work — the operations have asymmetric costs. Append — O(1) amortised. Most appends are cheap; occasional ones trigger a resize. The 'amortised' is key — averaging over many appends, each is constant. Index — O(1). Direct memory offset. Same speed as a C array. Insert at position i — O(n). Every element after i shifts right by one slot. Insert at position 0 in a list of 1M items moves a million pointers. Delete at position i — O(n). Same shift, in the other direction. Search by value — O(n). Lists have no idea what they contain. Linear scan. Search by sorted value — O(log n) with bisect, but ONLY if the list is sorted. Otherwise O(n). The asymmetry guides design. Append is cheap; insert-at-front is expensive. If you find yourself inserting at the front a lot, switch to collections.deque — same iteration interface, O(1) appendleft and popleft. If you're searching by value a lot, the list is wrong; you want a set or dict. The 'list is dynamic array' frame answers most performance questions about lists in advance. Hold it; use it.
#DSA#DataStructures#Algorithms#Python#100DaysOfCode#Arrays