Menu

Python Lists: How to Create, Index, Append, Sort, and Iterate

A working tour of Python lists — the most common collection — covering creation, indexing, slicing, append, sort, and the patterns you'll reach for most.

Lists Are Ordered, Mutable Sequences

The list is the single most-used collection type in Python. If you're keeping an ordered set of values that might change — add, remove, sort, update — a list is almost always the right choice.

You create one with square brackets:

main.py
Output
Click Run to see the output here.

Lists can hold anything, even other lists. In practice, most lists end up holding one kind of thing — a list of users, a list of numbers — because that's what's easiest to reason about.

Indexing

Each item has a position, starting at 0. Access with square brackets:

main.py
Output
Click Run to see the output here.

Asking for an index that doesn't exist raises IndexError. If you might be out of range, either check the length first or wrap the access in a try.

Slicing

Slicing gives you a range of items. The syntax is list[start:stop:step], same as strings:

main.py
Output
Click Run to see the output here.

Two things worth locking in:

  • Slicing always returns a new list. Modifying the slice doesn't affect the original.
  • The stop index is exclusive — nums[2:5] gives indices 2, 3, and 4, not 5.

How to Append, Extend, and Insert Items

The three "adding to a list" methods do different things, and mixing them up is a common source of bugs:

main.py
Output
Click Run to see the output here.
  • append(x) adds x as a single element — even if x is a list. items.append([1, 2]) adds the list itself as one entry.
  • extend(iterable) adds each item from the iterable. items.extend([1, 2]) adds two entries.
  • insert(i, x) inserts at a specific index, shifting everything at or after i one position to the right.

+= with a list on the right works like extend:

main.py
Output
Click Run to see the output here.

And for removing:

main.py
Output
Click Run to see the output here.

If you try to remove() something that isn't in the list, Python raises ValueError. If you need "remove if present," either check in first or use a try.

You can also delete by index with del:

main.py
Output
Click Run to see the output here.

Length, Membership, Counting

main.py
Output
Click Run to see the output here.

in is the readable way to check whether a list contains a value. If you'll be asking that question many times over a large list, use a set instead — set membership is O(1) where list membership is O(n).

Sorting

Two ways, and the choice matters:

main.py
Output
Click Run to see the output here.

Both accept reverse=True for descending order:

main.py
Output
Click Run to see the output here.

And a key function for custom ordering:

main.py
Output
Click Run to see the output here.

key is applied to each item, and the result is what gets compared. Common cases: sorting by length, by a specific attribute, or by the lowercase form of a string.

Reversing

Three flavours, depending on what you need:

main.py
Output
Click Run to see the output here.

Use in-place when you don't need the original. Use slicing when you want a new copy. Use reversed() when you just need to iterate in reverse without materializing a new list.

The Shared-Reference Trap

Lists are mutable, and variables are references. That means two variables can point at the same list:

main.py
Output
Click Run to see the output here.

b = a didn't copy the list; it made b a second name for the same list. If you want a copy, ask for one:

main.py
Output
Click Run to see the output here.

This bites every Python programmer at least once. Remember: = between two lists doesn't copy.

Looping and Building

You already know the basic pattern from the for loop page:

main.py
Output
Click Run to see the output here.

That's readable and will always be fine. Once you're comfortable, the list comprehension equivalent does the same thing in one line:

main.py
Output
Click Run to see the output here.

We'll cover comprehensions in detail two pages from now.

A Quick Cheat Sheet

Methods worth knowing, at a glance:

  • append(x) — add to end
  • extend(iter) — add each item from an iterable
  • insert(i, x) — insert at index i
  • pop() / pop(i) — remove and return
  • remove(x) — remove first occurrence of x
  • sort() / sort(key=...) / sort(reverse=True)
  • reverse()
  • index(x) — position of first x
  • count(x) — number of x's
  • copy() — shallow copy

Up Next

Lists are the workhorse. Next we'll look at tuples — their immutable cousin — and when reaching for a tuple instead is the better call.

Frequently Asked Questions

How do I create a list in Python?

Use square brackets with comma-separated values: fruits = ['apple', 'banana', 'cherry']. Lists can hold any type, and you can mix types in the same list, though most real lists are homogeneous by habit.

How do I sort a list in Python?

Call .sort() on the list to sort it in place, or use the sorted(list) built-in to get a new sorted list without changing the original. Both accept a reverse=True flag for descending order and a key= argument for custom sort keys.

How do I reverse a list in Python?

list.reverse() reverses in place. list[::-1] returns a new reversed list. reversed(list) returns an iterator you can loop over. Pick whichever matches your need: in-place vs. new copy vs. lazy iteration.

Learn to code with Coddy

GET STARTED