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. Under the hood, arrays are stored in a contiguous chunk of memory large enough to hold that many elements.

// declare an array that holds 3 integers int gasPrices[3]; gasPrices[0] = 346; gasPrices[1] = 360; gasPrices[2] = 354;

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, which are usually easier to work with. In Python, we call them "lists."

A dynamic array doesn’t require you to specify its 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:

gas_prices = [] gas_prices.append(346) gas_prices.append(360) gas_prices.append(354)

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.

See also:

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.

Psst. Pass it on.

. . .