An array is a data structure that holds an ordered collection of elements. Each position in the array has an index, starting with 0.
In a low-level array, you must specify the size of your array when you instantiate it. Arrays are implemeted by allocating a contiguous chunk of memory large enough to hold that many elements.
Arrays are efficient for looking up the element at an index, because if you know the address where an array starts in memory, it’s simple math to find the address of any index. This gives arrays an lookup time.
Arrays are the foundation of many other data structures, like dynamic arrays, stacks, and dictionaries .
Some languages, including Python, have dynamic arrays with a few additional features. In Python, we call them "lists."
A dynamic array doesn’t require you to specify the length and allows you to seamlessly (although sometimes with time and space costs) insert and delete elements at any index.
In Python, you can simply say:
Here, the details about the length are abstracted out for you. You can add as many prices as you’d like.
Fun fact: strings are almost always implemented as arrays of characters.
Pass Your Interviews with My FREE 7-Day Crash Course
I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.
No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe if you hate it.