Skip to content
Forge Learn/Core Collections
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Slicing, Iteration & Comprehensions

7 min read

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, called 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]

Try it yourself

Slicing follows the exact same "stops before, does not 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

Try it yourself

A list comprehension, written as [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 is not new capability. The manual loop above does exactly the same thing. It is a more compact, more readable way to express a pattern you will write extremely often: transform and/or filter a collection into a new one.

Dict and Set Comprehensions

Try it yourself

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 are 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 is 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 is also, as it happens, exactly enough to attempt a real, graded Forge Algorithm Lab level for the first time.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

You have now covered everything Level 1, "Big-O Detective," actually needs: functions, loops, lists, and dicts. No classes required yet; that is 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 do not 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 do not 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.