📘 CodingMarble Learn

Lists in Python: Create, Traverse, Change and List Methods

A list is an ordered, changeable collection written in square brackets, like [10, 20, 30]. Items are reached by index (from 0, or negative from the end) and slices. We traverse a list with a for loop, change items in place because lists are mutable, and use functions (len, max, min, sum, sorted, list) and methods (append, insert, extend, remove, pop, sort, reverse, count, index, clear) to work with it.

🎬 Step-by-step story

  1. A list is a row of boxes in square brackets. Each box has an index from 0.
  2. Pick one box with L[i], count from the end with L[-1], cut a slice with L[a:b].
  3. Traverse the list with a for loop to visit every item.
  4. Lists are mutable: change a box in place, join with +, repeat with *.
  5. Methods like append, insert, pop, remove and sort change the list.
  6. Your turn: tap a method and watch the list change.

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

🤔 Common doubts, cleared

Why does indexing start at 0?

The index is how far the item is from the start. The first item is 0 steps away.

Why is L[1:3] only two items?

The stop index is never included, just like range. 3 − 1 = 2 items.

Should I loop by item or by index?

By item (for x in L) to just read. By index (for i in range(len(L))) when you need the position or want to change L[i].

If L2 = L and I change L2, does L change?

Yes. Both names point to the same list. Use L2 = L.copy() or L[:] for a separate copy.

What is the difference between append and extend?

append adds one item (a list goes in as one box). extend adds each item of the given list separately.

Creating a list

A list is an ordered collection of items in [ ], separated by commas.

marks = [72, 85, 90]
mix = ["Ravi", 16, 7.5, True]   # different types allowed
empty = []
nested = [1, [2, 3]]            # a list inside a list
L = list("abc")                 # ["a", "b", "c"]
nums = list(range(1, 6))        # [1, 2, 3, 4, 5]

You can also build one from input: L = eval(input()) or by appending in a loop.

Indexing and slicing

Each item has an index: 0, 1, 2… from the left, and −1, −2… from the right.

L[i] gives one item. A wrong index gives IndexError.

Slicing L[start:stop:step] gives a new list from start up to stop − 1. L[:2] = first two, L[2:] = from index 2 on, L[::-1] = reversed copy.

Traversing a list

Visiting each item once is called traversal.

for x in L:              # by item
    print(x)

for i in range(len(L)):  # by index
    print(i, L[i])

Use the index form when you need the position or want to change items.

Changing a list and list operators

Lists are mutable: L[2] = 35 changes an item; L[0:2] = [1, 1] changes a slice; del L[1] deletes an item.

Built-in functions for lists

List methods

Most methods change the list and return None. So L = L.sort() makes L None!

Try it: class marks list

Write the marks of 5 friends in a list. In Python: print the highest, lowest and average (sum(L)/len(L)). Append a new friend's marks, sort the list, then remove the lowest with L.pop(0). Predict each result first, then check with step 6 of the 3D.

Key formulas and definitions

Worked examples

1. L = [5, 8, 2, 9]. Give L[1], L[-1] and L[1:3].

L[1] = 8, L[-1] = 9, L[1:3] = [8, 2].

2. L = [1, 2]. Compare L.append([3, 4]) and L.extend([3, 4]).

append adds the whole list as ONE item: [1, 2, [3, 4]] (length 3). extend adds each item: [1, 2, 3, 4] (length 4).

3. L = [3, 1, 4, 1, 5]. Find L.count(1), L.index(4) and sorted(L).

count(1) = 2, index(4) = 2, sorted(L) = [1, 1, 3, 4, 5] (L itself unchanged).

4. Write a program to find the average of marks [72, 85, 90, 65].

L = [72, 85, 90, 65] print(sum(L) / len(L)) → 312 / 4 = 78.0

5. Count how many even numbers are in L = [4, 7, 10, 3, 8].

c = 0 for x in L: if x % 2 == 0: c += 1 print(c) → 3 (4, 10, 8).

6. L = [10, 20, 30, 40]. What is L after: L.pop(1); L.insert(0, 5); L.remove(40)?

pop(1) removes 20 → [10, 30, 40]; insert(0, 5) → [5, 10, 30, 40]; remove(40) → [5, 10, 30].

Common mistakes

Practice quiz

1. L = [4, 6, 8]. L[-2] is:
2. Which adds several items to the end?
3. [1, 2] * 2 gives:
4. Which returns a new sorted list and keeps the old one?
5. L = [5, 6, 7]; L[3] gives:

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 a list in Python?

An ordered, mutable collection of items in square brackets, like [1, "a", 2.5].

What is the difference between pop() and remove()?

pop(i) removes the item at position i (last by default) and returns it; remove(x) removes the first item equal to x and returns None.

How do you traverse a list?

With a for loop: for x in L: … or for i in range(len(L)): … L[i] …

Where this is taught

Canada (Ontario)Grade 12A. Programming Concepts and Skills
RomaniaClasa a IX-aStoring data and organising code in a programming language
RomaniaClasa a IX-aStoring data and organising code in a programming language
RomaniaClasa a IX-aStoring data and organising code in a programming language
Ukraine10 класProgramming language and data structures
CBSE (India)Class 11Computational Thinking and Programming - 1
CBSE (India)Class 11Introduction to Python
USA (Common Core, NGSS, AP)Grade 10Big Idea 3: Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 11Data Collections
South Korea고등학교 3학년Programming
Russia10 классAlgorithms and programming
Russia11 классAlgorithms and programming

Learn first

Learn next

Related lessons

All Informatics Practices lessons