LevelJar Sign in

Python course / Foundations

Lists

Build, slice, sort and filter lists, and handle grids made of them. Two names for one list will change your data behind your back, and that is what most list bugs turn out to be.

  • indexing
  • slicing
  • sorting
  • copies vs names
  • filtering
  • grids
  • stacks and queues
  • unpacking

Start this module

Runs in your browser. Start straight away; create an account when you want your progress kept.

filtering, sorting and joining
stock = ["tea", "jam", "honey", "oats", "jam"]

kept = [name for name in stock if name != "jam"]
kept.sort()

print("kept:", kept)
print("as text:", ", ".join(kept))
print("first two:", kept[:2])
print("jam appears", stock.count("jam"), "times")
kept: ['honey', 'oats', 'tea']
as text: honey, oats, tea
first two: ['honey', 'oats']
jam appears 2 times

What you will learn

The lessons in this module

  • Making a list
  • Reaching into a list
  • Changing a list
  • Growing a list
  • Removing items
  • Slicing a list
  • Two names, one list
  • Changing a list while looping over it
  • Finding things in a list
  • Sorting
  • Numbers from a list
  • Between text and lists
  • Lists inside lists
  • Looping over rows
  • Building a list in a loop
  • Changing items by position
  • Pairing two lists
  • Filtering a list
  • Reversing
  • Stacks and queues
  • Unpacking a list
  • Removing duplicates
  • Replacing a section
  • Module review: playlist

Ready when you are

Start this module

Previous: Loops. Next: Dictionaries, sets, tuples. All modules are on the course page.