Python Academy · Lesson

Modifying Lists

Append, insert, remove, and sort list elements.

Lesson 2 of 414 steps

Modifying Lists is a free Python Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

Introduction

Lists are mutable — you can add, remove, and rearrange elements after creation.

append()

my_list.append(x) adds x to the end in O(1). It modifies the list in place and returns None.
lst = [1, 2]
lst.append(3)
print(lst)

insert()

my_list.insert(i, x) inserts x before index i. insert(0, x) prepends. O(n) due to shifting.
lst = [1, 3]
lst.insert(1, 2)
print(lst)

extend()

my_list.extend([4,5]) appends all elements of the iterable. Equivalent to my_list += [4,5].
lst = [1, 2]
lst.extend([3, 4])
print(lst)

remove()

my_list.remove(x) removes the first occurrence of x. Raises ValueError if x is not in the list.
lst = [1, 2, 3, 2]
lst.remove(2)
print(lst)

pop()

my_list.pop() removes and returns the last element. my_list.pop(0) removes the first. O(1) for end, O(n) for front.
lst = [1, 2, 3]
print(lst.pop())
print(lst)

del Statement

del my_list[1] removes element at index 1. del my_list[1:3] removes a slice.
lst = [1, 2, 3, 4]
del lst[1]
print(lst)

sort() and sorted()

lst.sort() sorts in place. sorted(lst) returns a new sorted list. Use key= for custom ordering.
lst = [3, 1, 4, 1, 5]
lst.sort()
print(lst)
print(sorted([3,1,2], reverse=True))

reverse()

lst.reverse() reverses in place. Returns None. To get a reversed copy use lst[::-1].
lst = [1, 2, 3]
lst.reverse()
print(lst)

count() and index()

lst.count(x) counts occurrences. lst.index(x) returns the first index — raises ValueError if absent.
lst = [1, 2, 2, 3]
print(lst.count(2))
print(lst.index(2))

clear()

lst.clear() removes all elements, leaving an empty list. Equivalent to del lst[:] but more readable.
lst = [1, 2, 3]
lst.clear()
print(lst)

Quick Check

Which method removes and returns the last element of a list?

Recap

Key list modifiers: append (end), insert (index), extend (iterable), remove (by value), pop (by index), sort, reverse, clear.

Keep Going

Great work! Move on to the next lesson to continue building your skills.
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “Modifying Lists” lesson free?

Yes — the full text of “Modifying Lists” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “Modifying Lists”?

Append, insert, remove, and sort list elements. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Modifying Lists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Accessing Lists
  2. Modifying Lists
  3. Tuples and Immutability
  4. Nested Lists and Iteration
← Back to Python Academy