List append ExamplesUse the append method to add elements to a list at the end. Append lines from a file to a list.
This page was last reviewed on Nov 9, 2022.
List, append. The append() method adds an element to the end of a list. To place an element somewhere else in the list, we instead call the insert method.
Shows a list
Def notes. With simple methods on the list, like append() and insert(), we can build up more complicated logic. Larger programs are built up of smaller parts.
List, strings
First example. Often in Python we use append() to initially populate a list—we start with an empty list. In this example we call append on an empty list.
Step 1 Here we create an empty list. A list can start empty, but will expand to fit elements as we add them.
Initialize list
Step 2 We call append() 4 times. The 4 elements we add to the list are stored in that order.
Shows a list
# Step 1: create empty list. list = [] # Step 2: append 4 numbers to the list. list.append(1) list.append(2) list.append(6) list.append(3) print(list)
[1, 2, 6, 3]
Append, len. As we append elements to a list, the length of the list changes. With len, a built-in method, we access the element count. This is updated as the list is changed.
animals = [] animals.append("cat") animals.append("dog") count = len(animals) # Display the list and the length. print(animals) print(count)
['cat', 'dog'] 2
Append file lines. Here we read a file's lines into a list. Each line is stripped of trailing whitespace and newlines. We use a loop to strip each line and append it to a list.
Result The ending list has no trailing newlines at the end of the string elements.
list = [] f = open("C:\\programs\\file.txt", "r") # Loop through all lines. for line in f.readlines(): # Strip each line to remove trailing newline. list.append(line.strip()) print(list)
Line 1. Line 2.
['Line 1.', 'Line 2.']
Append, benchmark. Python lists have an internal implementation that seems to be optimized for append() calls. When we time append and insert, append is faster.
List for
Version 1 Append to the list at its end. We begin with a 1-element list that is re-created many times.
Version 2 Insert to the list at its beginning—call insert with a first argument of 0.
Result Append is faster than insert. This difference could become larger as lists become larger, as more elements need to be moved.
import time data = ["bird", "cat", "frog"] print(time.time()) # Version 1: append at end. for i in range(10000000): test = ["test"] for value in data: test.append(value) print(time.time()) # Version 2: insert at index 0. for i in range(10000000): test = ["test"] for value in data: test.insert(0, value) print(time.time())
1592756353.044928 1592756353.481425 = 0.436 s append 1592756354.048237 = 0.566 s insert 0
Append, insert, extend. With insert() we can place an element anywhere in the list. With extend() we can place elements from another list at the end.
List extend
A review. We added elements to the end of lists in Python. In benchmarking, we found append appears to be faster than insert. We reasoned about list performance.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.