Rows and Columns with Lists

A table is a list of lists, and a column is what you pull out of it.

A table is a list of rows

Each inner list is one row. Indexing gives you a row; indexing twice gives you one cell. The order of the columns is a decision you have to remember, which is exactly the weakness the next lesson fixes.

table = [
    ["Ada", 92, "maths"],
    ["Grace", 88, "computing"],
]

print(table[0])
print(table[0][1])

A column is a comprehension

There is no column object here. To work on one you build a new list from the same position in every row.

table = [["Ada", 92], ["Grace", 88], ["Alan", 79]]
scores = [row[1] for row in table]
print(scores)

Exercise

Try It Yourself

Pull the second column out of table into a list called scores, then print it.

Press Run to see output

Check Your Understanding