Chapter 3 · Data

Comprehensions and sorting

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

A comprehension builds a list

In this lesson you build lists in one line, and sort them by any key you choose.

This loop builds a list:

doubled = []
for x in nums:
    doubled.append(x * 2)

A comprehension writes that loop in one line:

doubled = [x * 2 for x in nums]

With nums = [1, 2, 3], both give [2, 4, 6].

squares = [n * n for n in range(5)]
print(squares)
print(len(squares), squares[-1])
words = ["a", "tree", "is", "green"]
long = [w for w in words if len(w) > 3]
print(long)
scores = [72, 45, 90, 38, 66]
passed = []
for s in scores:
    if s >= 50:
        passed.append(s)
print(passed)
# retry: each score under 50, plus 10
words = ["sun", "moon", "star", "sky"]
lengths = {w: len(w) for w in words}
print(lengths)
sizes = {len(w) for w in words}
print(sorted(sizes))
scores = [72, 45, 90]
print(any(s < 50 for s in scores))
print(all(s < 100 for s in scores))
print(sum(s for s in scores if s >= 50))
print(any(s == 100 for s in scores))
scores = [55, 100, 48, 81]
# print the three lines of the task
xs = [3, 1, 2]
ys = sorted(xs)
zs = [3, 1, 2]
zs.sort()
print(xs, ys, zs)
ws = [3, 1, 2]
ws = ws.sort()
print(ws)
print(sorted(["bob", "Cy", "ann"]))
pairs = [("Ada", 72), ("Bo", 95), ("Cy", 88)]
ranked = sorted(pairs, key=lambda p: p[1], reverse=True)
print(ranked)
best = max(pairs, key=lambda p: p[1])
print(best[0])
words = ["kiwi", "fig", "banana", "pear"]
print(sorted(words, key=len))
scores = [("Ada", 72), ("Bo", 95), ("Cy", 88),
          ("Di", 60), ("Ed", 91)]
# sort by score, highest first; print the top 3
words = ["sky", "planet", "sun", "comet", "moon"]
# the three lines of the task
pairs = [("Cy", 88), ("Ada", 95), ("Bo", 88), ("Di", 95)]
for p in pairs:
    print(p[0], p[1])
Terminal
Step 1 of 11Squares

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)