TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 9 of 22 · By Ethan Cole · Updated November 21, 2026

Python Lists: Create, Change, Sort and Slice

A list holds an ordered sequence of values in square brackets: ["Python", "SQL"]. Lists can grow and shrink, may contain duplicates, and can mix types, although in practice most lists hold one kind of thing. They are the collection you will reach for most often.

Reading from a list

Indexing and slicing work exactly as they do for strings: langs[0] is the first item, langs[-1] the last, and nums[1:3] returns a new list with the items at positions 1 and 2. len(langs) counts the items and x in langs checks membership.

Changing a list

Unlike strings, lists are mutable: you can change them in place.

  • append(x) adds one item at the end.
  • insert(i, x) places an item before position i.
  • remove(x) deletes the first item equal to x (and raises ValueError if there is none).
  • pop() removes and returns the last item; pop(i) removes position i.
  • lst[i] = x replaces one item.
  • sort() sorts the list in place and returns None. The built-in sorted(lst) returns a new sorted list and leaves the original alone. Both accept reverse=True.
Python 3 · editable

Expected output

['Python', 'JavaScript', 'SQL', 'HTML']
4 JavaScript HTML
HTML ['Python', 'JavaScript']
[1, 2, 5, 9] [9, 5, 2, 1]
[2, 5]
[1, 2, 5, 9, 100]

Names share the same list

The last three lines of the example contain the most common list surprise. copy = nums does not copy anything; it makes a second name for the same list object. Appending through copy therefore changes what nums shows. To get an independent list, write nums.copy(), list(nums) or nums[:]. These are shallow copies: a list of lists still shares the inner lists, and the copy.deepcopy() function handles that case.

For the complete method list with examples, see the lists and dictionaries cheat sheet.

Common mistakes

  • Writing nums = nums.sort(). sort() returns None, so you lose the list. Call nums.sort() on its own line, or use sorted().
  • Using append to add several items: a.append([4, 5]) adds one nested list. Use a.extend([4, 5]).
  • Reading past the end: a[10] on a short list raises IndexError, though slices like a[5:10] simply return fewer items.

Summary

  • Lists are ordered, mutable and written with square brackets.
  • append, insert, remove, pop and sort change a list in place.
  • sorted() returns a new list; sort() returns None.
  • Assignment shares a list; use .copy() for an independent one.

Quiz: Lists

1. After a = [1, 2] and a.append([3, 4]), what is len(a)?

append adds exactly one item, here the list [3, 4], so there are three items.

2. What does nums.sort() return?

sort() works in place and returns None; sorted() returns a new list.

3. After b = a and b.append(9), what happens to a?

Assignment never copies; a and b are two names for one list.

4. Which call removes and returns the last item?

pop() with no argument removes the last item and returns it.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.