4  Lists

x = [23, 9, 7, 9]
x
[23, 9, 7, 9]
type(x)
list

4.1 append(): add item to end of list

x.append(12)
x
[23, 9, 7, 9, 12]

4.2 extend(): Append elements of another iterable

z = [19, 21]
x.extend(z)
x
[23, 9, 7, 9, 12, 19, 21]

4.3 pop(): Remove and return element of list by index

Defaults to the last element

x
[23, 9, 7, 9, 12, 19, 21]
x.pop()
21
x
[23, 9, 7, 9, 12, 19]

Remove and return the 5th element:

x.pop(4)
12

4.4 insert(): Insert element before index

Note the function requires two inputs, there is no default location to insert

x.insert(4, 13)
x
[23, 9, 7, 9, 13, 19]

4.5 remove(): remove first occurence of value

x
x.remove(13)
x
[23, 9, 7, 9, 19]

4.6 count(): Count occurences of a value in the list

x.count(9)
2

4.7 copy(): make a (shallow) copy of the list

z = x.copy()
z
[23, 9, 7, 9, 19]

4.8 reverse(): Reverse elements in-place

x.reverse()

4.9 sort(): sort list in-place

Defaults to ascending order

x.sort()
x
[7, 9, 9, 19, 23]

5 sort in descending order with reverse:

x.sort(reverse=True)
x
[23, 19, 9, 9, 7]

5.1 clear(): remove all list items

x.clear()
x
[]

5.2 2-D List

x = [[1, 3, 5], [21, 25, 29]]
x
[[1, 3, 5], [21, 25, 29]]
len(x)
2

5.3 Repeat and append with * and +

["A", "B"] * 3
['A', 'B', 'A', 'B', 'A', 'B']
[21, 22] + [31, 32]
[21, 22, 31, 32]
[1, 2] * 2 + [3, 4] * 3
[1, 2, 1, 2, 3, 4, 3, 4, 3, 4]