Beginner Topic 18 of 54 Programming Fundamentals
Arrays and Lists
Ordered collections of elements accessed by numeric index.
How it works
Arrays store elements contiguously in memory, so the element at index i is found by simple address arithmetic in O(1) time. Lists in high-level languages are usually dynamic arrays that grow automatically by reallocating a larger block and copying, giving amortized O(1) appends. Inserting or deleting in the middle costs O(n) because later elements must shift.
Step-by-step walkthrough
- Elements sit side by side and are numbered from index 0.
- Reading a[2] jumps directly to 30 in constant time.
- Appending places a value at the end.
- Inserting in the middle shifts later elements to the right.
- Searching an unsorted list means checking one by one.
Where it is used
Storing scores, rows of data, queues of tasks, pixel buffers and lookup tables all use arrays. Because contiguous memory is cache-friendly, arrays are often the fastest general-purpose container.
Common pitfall
Indexing out of bounds, and assuming that copying a list copies its contents when it only copies the reference, are frequent mistakes. Removing items while iterating skips elements, and searching an unsorted list repeatedly is slow; use a set or dictionary for membership tests.
Where it fits in the course
Arrays and Lists is topic 18 of 54 in Programming Fundamentals, rated Beginner. It follows Debugging Basics. The next topic is Strings.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Arrays and Lists?
Where do people use Arrays and Lists?
What is the most common mistake with Arrays and Lists?
What should I learn before this topic?
How can I practice it?
Is Arrays and Lists hard to learn?
How long does it take to learn Arrays and Lists?
Which topics are related to Arrays and Lists?
What should I study after Arrays and Lists?
Related topics
- Debugging Basics (Programming Fundamentals)
- Strings (Programming Fundamentals)
- Comments and Documentation (Programming Fundamentals)
- Dictionaries and Maps (Programming Fundamentals)
- Arrays (Data Structures)
- Dynamic Arrays (Data Structures)
- Linked Lists (Data Structures)