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
Runs in your browser. Start straight away; create an account when you want your progress kept.
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
- Hold several values under one name.
- Add items one at a time, or several at once.
- Tell a copy from a second name for the same list.
- Sort in place or into a new list, and choose what to sort by.
- Split text into a list, and join a list back into text.
- Build a list of the items that match, and a cleaned-up version of the rest.
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
Previous: Loops. Next: Dictionaries, sets, tuples. All modules are on the course page.