Slicing, Iteration & Comprehensions
You'll learn to
- -Slice a sequence with the start:stop:step syntax
- -Write a list comprehension, including one with a filtering condition
- -Recognize dict and set comprehensions
You now know how to index a single item out of a list or string. This chapter covers grabbing a whole chunk at once (slicing), and then the single most distinctively "Pythonic" piece of syntax in the entire language: the comprehension - a way to build a new collection out of an existing one in a single, readable line.
Slicing: arr[start:stop:step]
Slicing follows the exact same "stops before, doesn't include" rule as range(): arr[1:4] gives you indices 1, 2, and 3 - three items, not four. Any of start, stop, or step can be omitted - a missing start defaults to the beginning, a missing stop defaults to the end. A step of -1 is the idiomatic way to reverse a sequence.
List Comprehensions
A list comprehension - [expression for item in iterable if condition] - builds a brand-new list by evaluating the expression once per item that passes the (optional) condition. It's not new capability - the manual loop above does exactly the same thing - it's a more compact, more readable way to express a pattern you'll write extremely often: transform and/or filter a collection into a new one.
Dict and Set Comprehensions
The same comprehension syntax extends to dicts ({key: value for ...}) and sets ({expression for ...}) - only the surrounding braces and what you produce per iteration change. All three forms share the same rule of thumb: reach for a comprehension when the logic is simple enough to read in one line, and fall back to a plain loop the moment a comprehension would need to be nested or would hurt readability.
Comprehensions are popular in real Python code because they're often faster than an equivalent manual loop and read as a single clear statement of intent - "give me all the X that satisfy Y." But a comprehension that needs two nested for clauses to explain is usually a sign to write it as a normal loop instead.
That's everything Phase 1's Tier 1 set out to teach - variables and types, control flow, and now the four collections (list, tuple, dict, set) plus the syntax to slice and transform them. That's also, as it happens, exactly enough to attempt a real, graded Forge Algorithm Lab level for the first time.
Given arr = [10, 20, 30, 40, 50], what does arr[1:4] return?
You've now covered everything Level 1, "Big-O Detective," actually needs - functions, loops, lists, and dicts. No classes required yet; that's still a tier away. The level hands you four small Python functions (finding a max, a binary search, and two different ways of finding duplicates) and asks you to return a dict mapping each function's name to its Big-O time complexity. You don't need to have memorized Big-O notation to give it an honest attempt right now - just trace through each function and count how many times it looks at the input as it grows. Get as far as you can, use the hints (the Blacksmith) if you get stuck, and don't worry about getting every answer right on the first try - a full, rigorous treatment of Big-O is coming very soon in Phase 2. This is your first real rep in the Forge editor: read the scenario, fill in the TODO, hit submit, and see what the hidden tests say.