📘 CodingMarble Learn

Lists in Python

A list is an ordered, mutable sequence of values of any type, written in square brackets: [4, 'a', 2.5]. Items are reached by index (0 from the left, −1 from the right) and can be changed in place. Operations: + (join), * (repeat), in / not in (membership) and slicing. Traversal uses for or while loops. Built-in functions and methods: len(), list(), append(), extend(), insert(), count(), index(), remove(), pop(), reverse(), sort(), sorted(), min(), max(), sum(). A nested list holds lists inside it, like a table (M[row][col]). Standard programs find the maximum, minimum and mean, do a linear search and count the frequency of elements.

🎬 Step-by-step story

  1. A list keeps many values in order inside square brackets. Each value has an index from 0.
  2. Lists can be joined with +, repeated with *, checked with 'in', and sliced.
  3. List methods change the list: append adds at the end, insert adds at a place, remove and pop take out, sort puts in order.
  4. A nested list is a list inside a list, like a table. M[1][2] is row 1, column 2.
  5. We can find the biggest, smallest and average. Linear search checks each item until it finds the key.
  6. Your turn: type numbers and a key, and watch the search.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Strings are immutable. Why can lists change?

A list stores references to its items and Python allows those slots to be replaced. Watch L[1] change colour without making a new list.

Is L[1:3] a new list?

Yes. Slicing always makes a new list; changing the slice does not change L.

What is the difference between remove and pop?

remove(7) looks for the value 7; pop(1) takes out whatever is at index 1 and gives it back to you.

How do I reach one cell of a nested list?

Use two indexes: first the row, then the column. M[1][2] picks row 1, then item 2 of that row.

When does linear search stop?

As soon as it finds the key (break), or after the last item if the key is not there.

Creating lists and indexing

L = [4, 7, 2, 5], E = [], M = [1, 'two', 3.0, [4, 5]], list('abc') → ['a', 'b', 'c'].

Indexing: L[0] = 4, L[-1] = 5. Lists are mutable: L[1] = 9 → [4, 9, 2, 5]. An index outside the list gives IndexError.

List operations

Traversing a list

for x in L:
    print(x)

for i in range(len(L)):
    L[i] = L[i] * 2     # change using index

i = 0
while i < len(L):
    print(L[i]); i += 1

List functions and methods

MethodWhat it does (L = [4, 7, 2])
len(L)3
L.append(6)adds one item at the end → [4, 7, 2, 6]
L.extend([8, 9])adds each item of another list
L.insert(1, 5)puts 5 at index 1
L.count(7)how many times 7 appears
L.index(7)first index of 7 (ValueError if missing)
L.remove(7)deletes the first 7
L.pop(i) / L.pop()removes and returns item at i / the last item
L.reverse()reverses in place
L.sort(), L.sort(reverse=True)sorts in place
sorted(L)returns a new sorted list
min(L), max(L), sum(L)2, 7, 13

del L[0] deletes by index; L.clear() empties the list.

Nested lists

A list can hold other lists: M = [[1, 2, 3], [4, 5, 6]]. M[1] is [4, 5, 6]; M[1][2] is 6. Traverse with nested loops:

for row in M:
    for x in row:
        print(x, end=' ')
    print()

Programs: max, min, mean, linear search, frequency

L = [4, 7, 2, 9, 5]
big = L[0]; small = L[0]
for x in L:
    if x > big: big = x
    if x < small: small = x
mean = sum(L) / len(L)        # 5.4

# linear search
key = 9; pos = -1
for i in range(len(L)):
    if L[i] == key:
        pos = i; break
print(pos)                    # 3

# frequency of each element
for x in set(L):
    print(x, L.count(x))

Linear search checks items one by one; in the worst case it looks at every item.

Key formulas and definitions

Worked examples

1. L = [10, 20, 30, 40]. Find L[1:3], L[-1], L * 2, 25 in L.

[20, 30]; 40; [10, 20, 30, 40, 10, 20, 30, 40]; False.

2. Output? L = [3, 1, 2]; L.append([4, 5]); print(len(L))

4, because append adds the whole list as one item: [3, 1, 2, [4, 5]].

3. Output? L = [3, 1, 2]; L.extend([4, 5]); L.insert(0, 9); L.remove(1); print(L)

[9, 3, 2, 4, 5]

4. Find max, min and mean of [12, 5, 8, 20, 15] without max/min.

big = small = 12; after checking all: big = 20, small = 5; mean = 60 / 5 = 12.0.

5. Linear search for 15 in [12, 5, 8, 20, 15]. How many comparisons?

12, 5, 8, 20, 15 → found at index 4 after 5 comparisons.

6. Swap elements at even positions with the next odd position in [1, 2, 3, 4, 5, 6].

for i in range(0, len(L) - 1, 2): L[i], L[i+1] = L[i+1], L[i] # [2, 1, 4, 3, 6, 5]

7. Count the frequency of each element in [2, 3, 2, 5, 3, 2].

Using a loop and count(): 2 → 3, 3 → 2, 5 → 1.

Common mistakes

Practice quiz

1. Which adds one item at the end?
2. L = [5, 6, 7]; L.pop() returns:
3. M = [[1, 2], [3, 4]]; M[1][0] is:
4. Lists are:
5. sorted(L) does what?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between append() and extend()?

append() adds its argument as one item; extend() adds each item of the given list separately.

What is a nested list?

A list that contains other lists as items, like [[1, 2], [3, 4]]; items are reached with two indexes.

How does linear search work?

It compares the key with each item from the start until a match is found or the list ends.

Where this is taught

Canada (Ontario)Grade 12A. Programming Concepts and Skills
CBSE (India)Class 11Computational Thinking and Programming - 1
CBSE (India)Class 11Introduction to Python

Learn first

Learn next

Related lessons

All Computer Science lessons