Data Structures

Python array Module — Complete Beginner to Interview Guide

Learn Python's array module from scratch: what it is, every method explained one by one with output, and how it compares to list and numpy for interviews.

  • Python
  • Array
  • DSA
  • Data Structures
  • Algorithm

1. What is the array Module?

Points to understand first:

  1. Python’s normal list can hold any mix of data types (numbers, text, other lists) at the same time.
  2. A list does this by secretly storing pointers (memory addresses) to each object, not the raw values themselves.
  3. The array module gives you a different kind of array — one that stores only one fixed type of value, as raw bytes, just like arrays in C or Java.
  4. Because of this, an array uses less memory than a list holding the same numbers.
  5. The trade-off: an array can only hold one type of value — you cannot mix numbers and text in it.
  6. array is part of the Python standard library — no installation needed, just import array.
what_is_array.py
array_type_restriction.py

2. Typecodes — Telling Python What Type to Store

Every array needs a typecode — a single letter that tells Python exactly what kind of value to store and how many bytes to use per item.

array.array typecodes
typecodeC typemin bytespython type
'b'signed char1int
'B'unsigned char1int
'h'signed short2int
'H'unsigned short2int
'i'signed int2int
'I'unsigned int2int
'l'signed long4int
'L'unsigned long4int
'q'signed long long8int
'Q'unsigned long long8int
'f'float4float
'd'double8float
typecodes_example.py

3. Creating an Array (Every Way)

3.1 From a List of Values

create_from_list.py

3.2 Empty Array

create_empty.py

4. Accessing and Slicing — Works Just Like a List

indexing_array.py

Important beginner gotcha: you cannot assign a plain list into an array slice — the right-hand side must also be an array of the same typecode.

slice_assignment_gotcha.py

5. Every Array Method — Explained One by One

5.1 append(x) — Add One Item to the End

append_array.py

5.2 insert(index, x) — Insert at a Specific Position

insert_array.py

5.3 extend(iterable) — Add Many Items at Once

extend_array.py

5.4 remove(x) — Remove the First Matching Value

remove_array.py

5.5 pop(index) — Remove and Return an Item

pop_array.py

5.6 index(x) — Find the Position of a Value

index_array.py

5.7 count(x) — Count How Many Times a Value Appears

count_array.py

5.8 reverse() — Reverse In Place

reverse_array.py

5.9 tolist() — Convert to a Regular Python List

tolist_example.py

5.10 fromlist(list) — Add Items From a Regular List

fromlist_example.py

5.11 tobytes() — Convert to Raw Bytes

When would you need this? When saving numbers to a file or sending them over a network in the most compact, raw binary form.

tobytes_example.py

5.12 frombytes(bytes_obj) — Load From Raw Bytes

frombytes_example.py

5.13 buffer_info() — Memory Address and Length

buffer_info_example.py

5.14 typecode and itemsize — Useful Attributes

typecode_itemsize.py

6. All Methods in One Table (Quick Reference)

Every array.array method at a glance
methodwhat it doestime complexity
append(x)Add x to the endO(1) amortized
insert(i, x)Insert x before index iO(n)
extend(iterable)Add each item of iterable to the endO(k)
remove(x)Remove first item equal to xO(n)
pop(i=-1)Remove & return item at index iO(1) at end, O(n) elsewhere
index(x)Find index of first xO(n)
count(x)Count occurrences of xO(n)
reverse()Reverse in placeO(n)
tolist()Convert to a regular Python listO(n)
fromlist(list)Add items from a regular listO(n)
tobytes()Convert to raw bytesO(n)
frombytes(b)Load items from raw bytesO(n)
buffer_info()Memory address + length of the bufferO(1)
typecode (attribute)The single-letter type code, e.g. 'i'O(1)
itemsize (attribute)Bytes used per elementO(1)

7. array vs list vs numpy.ndarray

Which one should a beginner actually use?
aspectlistarray.arraynumpy.ndarray
Can mix data types?YesNo — one type onlyNo — one dtype only
Memory usageHighest (stores pointers)Lower (raw values)Lower (raw values)
Math on the whole array at once?No — needs a loopNo — needs a loopYes — vectorized, very fast
Needs installing?Built-inBuilt-inpip install numpy
Best forGeneral everyday codeCompact single-type buffers, file/network I/ONumeric & scientific computing

Honest beginner advice:

  1. In most interview questions, you will use a normal list — it’s the default, flexible choice.
  2. array is rarely required directly in interviews — but understanding it helps you understand memory and typed data.
  3. numpy is what you reach for when you need fast math on lots of numbers at once (covered in the next guide).

8. Interview Patterns (Question First, Then Solution)

8.1 Pattern: Running Totals (Prefix Sum on an Array)

❓ Interview Question:

  1. Problem: Given an array of integers, return a new array where each position holds the running total up to that point.
  2. Example:
    • Input: array('i', [1, 2, 3, 4])
    • Expected Output: array('i', [1, 3, 6, 10])
  3. Hint: Keep a running variable that adds up as you go, just like with a list.

✅ Approach:

  1. Create a new array of the same length, filled with zeros to start.
  2. Loop through the input, keeping a running sum.
  3. Store the running sum at each position of the new array.
running_totals.py

Complexity: Time = O(n), Space = O(n) for the new result array.

8.2 Pattern: Find the Maximum Value Without Using max()

❓ Interview Question:

  1. Problem: Given an array of integers, find the largest value without using the built-in max() function (interviewers often ask this to test your understanding of loops).
  2. Example:
    • Input: array('i', [3, 7, 2, 9, 4])
    • Expected Output: 9
  3. Hint: Start by assuming the first item is the largest, then check every other item against it.

✅ Approach:

  1. Set largest = values[0] as a starting guess.
  2. Loop through the rest of the array.
  3. Any time you find a bigger value, update largest.
find_max_manual.py

Complexity: Time = O(n), Space = O(1).

9. Practice Problems (Try These Yourself)

  1. Write a function that reverses an array without using .reverse().
  2. Convert an array of Celsius temperatures into an array of Fahrenheit temperatures.
  3. Find the second-smallest value in an array.
  4. Merge two sorted arrays into one sorted array.

10. Key Takeaways

  1. array.array(typecode, values) stores one fixed C-type of value — more memory-efficient than a list, but less flexible.
  2. It shares most of a list’s methods (append, pop, insert, remove, index, count, reverse, slicing).
  3. Extra array-only methods handle binary conversion: tobytes(), frombytes(), tolist(), fromlist(), buffer_info().
  4. Slice assignment requires another array on the right side, not a plain list — a common beginner error.
  5. In real interview settings, list is still the default choice — array mainly matters for compact, single-type numeric storage and binary I/O.