liveBook · Manning

Built-in function

As explained in The Quick Python Book, Fourth Edition

append is a Python list method that adds one object to the end of a list.

append is a Python list method for adding one object to the end of an existing list. It changes the list in place and is suited to adding a single item. section 5.3

Keep these rules in mind:

  • Call it with dot notation: list.append(object). The argument becomes one element at the list's end. section 5.3
  • Appending another list adds that list as a nested element rather than merging its contents. section 5.3
  • Use extend when the elements of one list should be added individually to another list. section 5.3
  • Use slice assignment when you need an alternative that appends several elements or performs broader list edits. section 5.3

Add one item

Starting with x = [1, 2, 3], the call x.append("four") produces [1, 2, 3, 'four'].

Avoid accidental nesting

With x = [1, 2, 3, 4] and y = [5, 6, 7], x.append(y) produces [1, 2, 3, 4, [5, 6, 7]], whereas x.extend(y) produces [1, 2, 3, 4, 5, 6, 7].

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the method, demonstrates its output, and contrasts nested-list behavior with extend and slice assignment.
Examples
Demonstrates appending a string, appending a list as one nested element, and extending a list element by element.
What it assumes
Assumes familiarity with lists and list index notation while treating list modification as the immediate context.
What you can do after
Apply it via Python list syntax to add individual objects and choose extend when list contents should be merged.
How it connects
Connects append with lists, list index notation, slice assignment, extend, and insert.

Also explained in

Compare how 2 books explain built-in function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.