Skip to Content
Think Python, 3rd Edition
book

Think Python, 3rd Edition

by Allen B. Downey
May 2024
Beginner to intermediate
328 pages
6h 1m
English
O'Reilly Media, Inc.
Content preview from Think Python, 3rd Edition

Chapter 9. Lists

This chapter presents one of Python’s most useful built-in types, lists. You will also learn more about objects and what can happen when multiple variables refer to the same object.

In the exercises at the end of the chapter, we’ll make a word list and use it to search for special words like palindromes and anagrams.

A List Is a Sequence

Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements.

There are several ways to create a new list; the simplest is to enclose the elements in square brackets ([ and ]). For example, here is a list of two integers:

numbers = [42, 123]
       

And here’s a list of three strings:

cheeses = ['Cheddar', 'Edam', 'Gouda']
       

The elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and even another list:

t = ['spam', 2.0, 5, [10, 20]]
       

A list within another list is nested.

A list that contains no elements is called an empty list; you can create one with empty brackets, []:

empty = []
       

The len function returns the length of a list:

len(cheeses)
       
3
       

The length of an empty list is 0.

The following figure shows the state diagram for cheeses, numbers, and empty:

Lists are represented by boxes with the word “list” outside and the numbered elements of the list inside.

Lists Are Mutable ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Think Python, 2nd Edition

Think Python, 2nd Edition

Allen B. Downey

Publisher Resources

ISBN: 9781098155421Errata Page