01Arrays (Python array module)
Watch at 0:53This one actually threw me off in a real interview.
Python arrays are not the same as lists. Python has a built-in array module that lets you create type-specific arrays.
You have to specify a type code when you create them, which means every element in the array must be the same type.
Why arrays matter:
- They are more memory-efficient than lists
- They enforce type consistency
- They’re faster when working with large datasets of the same type
Trade-offs:
- Can only store one type
- Less flexible than lists
- Not dynamically typed like lists
Use arrays when:
- You care about memory
- You’re working with large numeric data
- You don’t need list flexibility
1.arrays.pyGitHub ↗from array import array
numbers = array('i', [1, 2, 3, 4, 5])
# i for integers
numbers.append(6) # Add element to end
numbers.extend([7, 8]) # Add multiple elements
numbers.insert(0, 0) # Insert at specific position
print(numbers)