List Methods#
Note
Source: Adapted from the C# edition (lists/listsyntax.rst and
arrays/onedim.rst). Python list methods replace C# List<T>
methods (Add, Remove, Contains, etc.). Built-in functions
sorted(), min(), max(), and sum() replace C# LINQ equivalents.
Python lists come with a rich set of methods for adding, removing, searching, and reordering elements.
Adding Elements#
append(x)addsxto the end — the most common way to grow a list.insert(i, x)insertsxbefore indexi.extend(other)appends all elements ofotherto the list.
words = ["up", "down"]
words.append("over")
print(words)
words.insert(1, "under")
print(words)
words.extend(["in", "out"])
print(words)
Output:
['up', 'down', 'over']
['up', 'under', 'down', 'over']
['up', 'under', 'down', 'over', 'in', 'out']
Removing Elements#
remove(x)deletes the first occurrence ofx; raisesValueErrorif not found.pop(i)removes and returns the element at indexi(default: last element).del lst[i]removes the element at indexiwithout returning it.
nums = [3, 1, 4, 1, 5]
nums.remove(1) # removes the first 1
print(nums)
last = nums.pop() # removes and returns 5
print(last, nums)
Output:
[3, 4, 1, 5]
5 [3, 4, 1]
Searching#
index(x)returns the index of the first occurrence ofx; raisesValueErrorif not found.count(x)returns how many timesxappears.
nums = [3, 1, 4, 1, 5]
print(nums.index(4)) # 2
print(nums.count(1)) # 2
Sorting and Reversing#
sort()sorts the list in place (ascending by default).reverse()reverses the list in place.
nums = [3, 1, 4, 1, 5, 9]
nums.sort()
print(nums)
nums.reverse()
print(nums)
Output:
[1, 1, 3, 4, 5, 9]
[9, 5, 4, 3, 1, 1]
Built-in Functions#
These built-in functions work on any iterable and do not modify the original list:
sorted(lst)returns a new sorted list.reversed(lst)returns an iterator over the list in reverse order.min(lst),max(lst)return the smallest and largest element.sum(lst)returns the sum of all elements (numbers only).
nums = [3, 1, 4, 1, 5]
print(sorted(nums)) # new list; nums unchanged
print(min(nums), max(nums), sum(nums))
Output:
[1, 1, 3, 4, 5]
1 5 14
Concatenation and Repetition#
The + operator concatenates two lists into a new one; *
repeats a list:
a = [1, 2]
b = [3, 4]
print(a + b)
print(a * 3)
Output:
[1, 2, 3, 4]
[1, 2, 1, 2, 1, 2]
Note that + is different from extend: + produces a new
list and leaves both operands unchanged, while extend mutates the
list it is called on.