Bird
Raised Fist0
Intro to Computingfundamentals~6 mins

Arrays and lists in Intro to Computing - Full Explanation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Imagine you have a collection of items, like a row of mailboxes or a shopping list. You need a way to keep these items organized so you can find, add, or change them easily. Arrays and lists are tools that help computers organize and manage groups of data in a simple, ordered way.
Explanation
Arrays
An array is like a row of boxes where each box holds one item. All boxes are the same size and type, so you know exactly what kind of item goes in each. You can quickly find an item by its position number, starting from zero. Arrays have a fixed size, so you decide how many boxes you need when you create it.
Arrays store items of the same type in a fixed-size, ordered sequence accessible by position.
Lists
A list is like a flexible chain of items where you can add or remove boxes anytime. Lists can hold different types of items and can grow or shrink as needed. Finding an item might take longer because you may need to look through the chain one by one. Lists are great when you don't know how many items you will have in advance.
Lists are flexible collections that can change size and hold different types of items.
Indexing and Access
Both arrays and lists use positions called indexes to find items. Indexes usually start at zero, so the first item is at position zero, the second at one, and so on. This numbering helps you quickly get or change an item without searching through everything.
Indexes starting at zero let you quickly access items by their position.
When to Use Each
Use arrays when you know the exact number of items and want fast access. Use lists when you need to add or remove items often or when the number of items can change. Choosing the right one helps your program run smoothly and handle data well.
Choosing arrays or lists depends on whether you need fixed size and speed or flexibility.
Real World Analogy

Think of an array as a row of identical mailboxes, each with a fixed number and size, where you can quickly find a letter by its mailbox number. A list is like a chain of shopping bags you can add to or remove from anytime, and you might have to look through them to find a specific item.

Arrays → A row of identical mailboxes with fixed size and numbered slots
Lists → A chain of shopping bags that can grow or shrink as needed
Indexing and Access → Mailbox numbers that help you find letters quickly
When to Use Each → Choosing mailboxes for fixed letters or bags for flexible shopping
Diagram
Diagram
┌─────────────┐
│   Arrays    │
├─────────────┤
│ [0] Item A  │
│ [1] Item B  │
│ [2] Item C  │
│ [3] Item D  │
└─────────────┘

┌─────────────┐
│    Lists    │
├─────────────┤
│ Item A →    │
│ Item B →    │
│ Item C →    │
│ Item D → ∅  │
└─────────────┘
The diagram shows an array as fixed boxes with numbered positions and a list as linked items pointing to the next.
Key Facts
ArrayA fixed-size collection of items of the same type stored in order.
ListA flexible collection that can grow or shrink and hold different types.
IndexA number starting at zero used to locate an item in an array or list.
Fixed SizeThe number of items in an array cannot change after creation.
Dynamic SizeLists can add or remove items, changing their size during use.
Common Confusions
Arrays and lists are the same because both hold multiple items.
Arrays and lists are the same because both hold multiple items. Arrays have a fixed size and hold items of the same type, while lists can change size and hold different types.
Indexing starts at 1 because it feels more natural.
Indexing starts at 1 because it feels more natural. Indexing starts at 0 in most programming languages to simplify calculations and access.
Summary
Arrays are fixed-size collections that store items of the same type in order and allow quick access by position.
Lists are flexible collections that can grow or shrink and hold different types of items, but access may be slower.
Choosing between arrays and lists depends on whether you need fixed size and speed or flexibility and dynamic size.

Practice

(1/5)
1. What is the position of the first item in an array or list?
easy
A. 0
B. 1
C. Depends on the list size
D. Last position

Solution

  1. Step 1: Understand list indexing

    Arrays and lists start counting positions from zero, not one.
  2. Step 2: Identify the first position

    The first item is always at position 0 in the list.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    First position = 0 [OK]
Hint: Remember counting starts at zero in lists [OK]
Common Mistakes:
  • Thinking first position is 1
  • Confusing position with size
  • Assuming last position is first
2. Which of the following is the correct way to add an item 'apple' to a list named fruits in Python?
easy
A. fruits.insert('apple')
B. fruits.add('apple')
C. fruits.append('apple')
D. fruits.push('apple')

Solution

  1. Step 1: Recall Python list methods

    To add an item at the end of a list, Python uses the append() method.
  2. Step 2: Match method to syntax

    fruits.append('apple') correctly adds 'apple' to the list.
  3. Final Answer:

    fruits.append('apple') -> Option C
  4. Quick Check:

    Use append() to add items [OK]
Hint: Use append() to add items at list end [OK]
Common Mistakes:
  • Using add() which is for sets
  • Using push() which is JavaScript
  • Using insert() without position
3. What will be the output of this Python code?
numbers = [10, 20, 30, 40]
print(numbers[2])
medium
A. 30
B. 20
C. 40
D. 10

Solution

  1. Step 1: Identify the index used

    The code accesses numbers[2], which means the item at position 2.
  2. Step 2: Find the item at index 2

    Positions start at 0: numbers[0]=10, numbers[1]=20, numbers[2]=30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Index 2 value = 30 [OK]
Hint: Count from zero to find index value [OK]
Common Mistakes:
  • Counting from 1 instead of 0
  • Confusing index with value
  • Choosing last item by mistake
4. Find the error in this Python code that tries to print the last item of a list named colors:
colors = ['red', 'green', 'blue']
print(colors[3])
medium
A. SyntaxError due to wrong brackets
B. IndexError because index 3 is out of range
C. prints 'blue' correctly
D. TypeError because colors is not a list

Solution

  1. Step 1: Check list length and index

    The list has 3 items at indexes 0, 1, and 2. Index 3 is beyond the last item.
  2. Step 2: Understand error type

    Accessing index 3 causes an IndexError because it is out of range.
  3. Final Answer:

    IndexError because index 3 is out of range -> Option B
  4. Quick Check:

    Index out of range = IndexError [OK]
Hint: Last index is length minus one [OK]
Common Mistakes:
  • Using wrong index number
  • Thinking index 3 is valid
  • Confusing error types
5. You have a list of daily temperatures: temps = [22, 19, 24, 21, 20]. How can you create a new list with only temperatures above 20 using Python list comprehension?
hard
A. [temp > 20 for temp in temps]
B. [temp for temp in temps where temp > 20]
C. [temp if temp > 20 for temp in temps]
D. [temp for temp in temps if temp > 20]

Solution

  1. Step 1: Understand list comprehension syntax

    The correct syntax is: [expression for item in list if condition].
  2. Step 2: Apply condition to filter temps

    Use temp for temp in temps if temp > 20 to select temps above 20.
  3. Final Answer:

    [temp for temp in temps if temp > 20] -> Option D
  4. Quick Check:

    Filter with if inside comprehension [OK]
Hint: Use if after for in list comprehension [OK]
Common Mistakes:
  • Placing if before for
  • Using > 20 inside list without if
  • Using where instead of if