Skip to content

Informatics Practices · Ch 4 — Working with Lists and Dictionaries

List Operations

4.2

List Operations

A list is not just a container to store values — the list data type lets you manipulate its contents through a set of built-in operations. Instead of writing loops for every routine task, Python gives lists a small toolkit of operators that combine, replicate, test and extract elements directly.

The operations the chapter takes up one by one are:

  • Concatenation — joining two or more lists into one, using the + operator.
  • Repetition — replicating the contents of a list a given number of times, using the * operator.
  • Membership — checking whether an element is present in a list (or absent from it), using the in and not in operators, which answer with True or False.
  • Slicing — creating a new list by taking out a chosen range of elements from an existing list.

Two things are worth noticing before diving into the details:

  • Familiar symbols, list-specific meaning. The operators + and * are the same symbols used for arithmetic, but applied to lists they do something different: + glues sequences together and * repeats them. Python decides what an operator does by looking at the type of its operands. …