Python Functions and Methods: A Topic-by-Topic Reference · Lesson 5 of 12

Lesson 5: List Methods: Adding, Removing and Searching

append, extend, insert, remove, pop, clear, index, count, copy, slicing and the difference between sort and sorted.

Lesson 5: List Methods: Adding, Removing and Searching

A list is an ordered, changeable collection. Most list methods change the list in place and return None, so never write x = x.append(1).

Adding items

append(x)

Adds one item to the end.

fruits = ["apple"]
fruits.append("mango")
print(fruits)

Output:

['apple', 'mango']

extend(iterable)

Adds every item of another iterable to the end. Compare with append, which would add the whole list as one item.

a = [1, 2]
a.extend([3, 4])
print(a)
b = [1, 2]
b.append([3, 4])
print(b)

Output:

[1, 2, 3, 4]
[1, 2, [3, 4]]

insert(i, x)

Puts an item at position i, shifting the rest right.

letters = ["a", "c"]
letters.insert(1, "b")
print(letters)
letters.insert(0, "z")
print(letters)

Output:

['a', 'b', 'c']
['z', 'a', 'b', 'c']

+ and *

Join two lists into a new one, or repeat a list.

print([1, 2] + [3])
print([0] * 5)

Output:

[1, 2, 3]
[0, 0, 0, 0, 0]

Removing items

remove(x)

Deletes the first item equal to x. Raises an error if it is not there.

nums = [5, 3, 5, 1]
nums.remove(5)
print(nums)

Output:

[3, 5, 1]

pop(i)

Removes and returns the item at i (the last one by default).

stack = [1, 2, 3]
print(stack.pop())
print(stack.pop(0))
print(stack)

Output:

3
1
[2]

del and clear()

del removes by position or slice; clear empties the list.

x = [10, 20, 30, 40, 50]
del x[0]
print(x)
del x[1:3]
print(x)
x.clear()
print(x)

Output:

[20, 30, 40, 50]
[20, 50]
[]

Searching

index(x)

The position of the first match. Raises an error if absent, so check with in first.

names = ["Ram", "Sita", "Hari"]
print(names.index("Sita"))
print("Gita" in names)

Output:

1
False

count(x)

How many times x appears.

print([1, 2, 2, 3, 2].count(2))

Output:

3

Copying

b = a does not copy a list: both names point to the same list. Make a real copy with copy(), list(a) or a[:].

copy()

A shallow copy: a new list holding the same items.

a = [1, 2, 3]
b = a
c = a.copy()
a.append(4)
print(b)
print(c)

Output:

[1, 2, 3, 4]
[1, 2, 3]

A shallow copy of a list of lists still shares the inner lists. Use copy.deepcopy(x) to copy everything.

Slicing

lst[start:stop:step]

A new list from part of the old one.

n = [0, 1, 2, 3, 4, 5, 6]
print(n[2:5])
print(n[:3])
print(n[-2:])
print(n[::2])
print(n[::-1])

Output:

[2, 3, 4]
[0, 1, 2]
[5, 6]
[0, 2, 4, 6]
[6, 5, 4, 3, 2, 1, 0]

Unpacking

Assign list items to names in one line; *rest collects the extras.

first, second, *rest = [10, 20, 30, 40]
print(first, second, rest)
*start, last = [1, 2, 3]
print(start, last)
a, b = 1, 2
a, b = b, a
print(a, b)

Output:

10 20 [30, 40]
[1, 2] 3
2 1

Try it yourself

  1. Remove all the duplicates from [3, 1, 3, 2, 1] while keeping the order (hint: loop and check with in).
  2. Rotate [1, 2, 3, 4, 5] by two places using slicing.
  3. Use pop to implement a stack (last in, first out).

Next lesson: sorting and transforming lists.

Finished this lesson?Log in to save your progress and pick up where you left off.
Next lesson →

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.