0%

Chapter 6

Lists

Meet Lists

A can hold one . That works well until several values belong together.

Imagine keeping three places you would like to visit:

Try it

This program works, but the variable names are doing too much work. Adding a fourth place means inventing another name. Treating all the places as one group is awkward.

A stores an ordered collection of values:

Try it

The square brackets mark the beginning and end of the list. Commas separate its items. The list itself is one value, so one variable can refer to the complete collection.

Order is part of the list

Python keeps list items in the order you write them:

Try it

That order can carry meaning. A packing list, a reading list, and a set of project steps are all useful examples of ordered collections.

Lists can hold values of different , but related values usually make a program easier to understand. A list of book titles has a clearer purpose than a list containing a title, an age, and a for no particular reason.

An empty pair of brackets creates an empty list:

Try it

An empty list is still a list. It simply contains no items yet.

Which value is a Python list?

A list gives several related values one ordered home. Next, you will create lists of your own and inspect what Python stores in them.