Chapter 3 · Data

Lists

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

Step 1 of 22 · Concept

A list keeps items in order

In this lesson you keep many values in one list, change it, and see when two names share it.

names = ["Ada", "Alan", "Grace"]
  • A list is values in [...], in order. Each cell holds an arrow to its object.
  • Index, slice and len work as on text: names[0] is "Ada", names[-1] is "Grace", len(names) is 3.
  • list(range(5)) is [0, 1, 2, 3, 4].

Like a name's box, a cell may show a number inside it (lesson 3's shorthand). Really, it holds an arrow.

names = ["Ada", "Alan", "Grace"]
names.append("Linus")
names.insert(0, "Ida")
names.remove("Alan")
names[0] = "Joan"
del names[1]
print(names.pop())
print(names)
shopping = ["milk", "eggs"]
# 1. add "bread", then "tea", at the end
# 2. remove "eggs"
# 3. put "apples" at the front
print(shopping)
print(len(shopping), "items")
scores = [70, 85, 90, 75]
sum = 0
for score in scores:
    sum += score
print("Total:", sum)
print("sum() says:", sum(scores))
print("Best:", max(scores), "Worst:", min(scores))
print("Average:", sum(scores) / len(scores))
print(85 in scores, 60 not in scores)
print(scores[0], scores[1:], list(range(5)))
scores = [45, 72, 50, 91, 38, 66]
over = []
# add each score over 50 to over
print(over)
print(sum(over))
a = [1, 2, 3]
b = a
c = a.copy()
print(a is b, a == c, a is c)
b.append(9)
print(a)
print(b)
print(c)
a = [1, 2]
b = a
a += [3]
a = a + [4]
print(a)
print(b)
def add(item, items=[]):
    items.append(item)
    return items


def add_safe(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items


first = add(1)
second = add(2)
print(first, second)
print(add_safe(1), add_safe(2))
def doubled(xs):
    # return a new list: each number times 2
    return xs


nums = [1, 2, 3]
twice = doubled(nums)
print(nums)
print(twice)
scores = [60, 90, 80, 70]
# a copy without the lowest, then both averages
names = ["Ada", "Alan", "Ada", "Grace", "Alan"]
unique = []
# add each name once, then print each count
print(unique)
Terminal
Step 1 of 10Changing a list

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)