append()
A list method that adds one item to the end of the list, changing the list in place.
In more detail
append returns None, so nums = nums.append(4) loses your list; just write nums.append(4). Building a list by starting with [] and appending in a loop is the list version of the accumulation pattern. + creates a new list instead.
Example
nums = []
nums.append(4)Related terms
- list — An ordered, changeable collection of values written in square brackets, such as [3, 1, 4].
- mutable — Able to be changed in place after it is created, like most lists, arrays and objects in languages such as Python and JavaScript.
- accumulation pattern — A loop pattern that builds a result step by step, starting from an initial value and combining each item into it, such as adding up a list.
- concatenation — Joining two strings or two lists end to end with +, as in "Hello, " + name.
Taught in
- Dictionaries And String Formatting — Hands-on Python: Complete Video Course & Book
- Objects, Methods And String Case — Hands-on Python: Complete Video Course & Book
- String Slices — Hands-on Python: Complete Video Course & Book
- Appending To Lists, Sets, Constructors — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: Finding The Cues — Hands-on Python: Complete Video Course & Book
- Paths And Files The Modern Way — Hands-on Python: Complete Video Course & Book
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Parameters, Arguments And Scope — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Classes, Objects And Two Stacks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- List Comprehensions, Filtered And Nested — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Closures: Functions That Remember — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Streams, Handles And Open Modes — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep