In this series of lectures we will be diving a little deeper into all the methods available in a list object. These aren't officially "advanced" features, just methods that you wouldn't typically encounter without some additional exploring. It's pretty likely that you've already encountered some of these yourself!
Let's begin!
list1 = [1,2,3]
You will definitely have used this method by now, which merely appends an element to the end of a list:
list1.append(4)
list1
[1, 2, 3, 4]
We discussed this during the methods lectures, but here it is again. count()
takes in an element and returns the number of times it occurs in your list:
list1.count(10)
0
list1.count(2)
1
Many times people find the difference between extend and append to be unclear. So note:
append: appends whole object at end:
x = [1, 2, 3]
x.append([4, 5])
print(x)
[1, 2, 3, [4, 5]]
extend: extends list by appending elements from the iterable:
x = [1, 2, 3]
x.extend([4, 5])
print(x)
[1, 2, 3, 4, 5]
Note how extend()
appends each element from the passed-in list. That is the key difference.
index()
will return the index of whatever element is placed as an argument. Note: If the the element is not in the list an error is raised.
list1.index(2)
1
list1.index(12)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-56b94ada72bf> in <module>() ----> 1 list1.index(12) ValueError: 12 is not in list
insert()
takes in two arguments: insert(index,object)
This method places the object at the index supplied. For example:
list1
[1, 2, 3, 4]
# Place a letter at the index 2
list1.insert(2,'inserted')
list1
[1, 2, 'inserted', 3, 4]
You most likely have already seen pop()
, which allows us to "pop" off the last element of a list. However, by passing an index position you can remove and return a specific element.
ele = list1.pop(1) # pop the second element
list1
[1, 'inserted', 3, 4]
ele
2
The remove()
method removes the first occurrence of a value. For example:
list1
[1, 'inserted', 3, 4]
list1.remove('inserted')
list1
[1, 3, 4]
list2 = [1,2,3,4,3]
list2.remove(3)
list2
[1, 2, 4, 3]
As you might have guessed, reverse()
reverses a list. Note this occurs in place! Meaning it affects your list permanently.
list2.reverse()
list2
[3, 4, 2, 1]
The sort()
method will sort your list in place:
list2
[3, 4, 2, 1]
list2.sort()
list2
[1, 2, 3, 4]
The sort()
method takes an optional argument for reverse sorting. Note this is different than simply reversing the order of items.
list2.sort(reverse=True)
list2
[4, 3, 2, 1]
A common programming mistake is to assume you can assign a modified list to a new variable. While this typically works with immutable objects like strings and tuples:
x = 'hello world'
y = x.upper()
print(y)
HELLO WORLD
This will NOT work the same way with lists:
x = [1,2,3]
y = x.append(4)
print(y)
None
What happened? In this case, since list methods like append()
affect the list in-place, the operation returns a None value. This is what was passed to y. In order to retain x you would have to assign a copy of x to y, and then modify y:
x = [1,2,3]
y = x.copy()
y.append(4)
print(x)
[1, 2, 3]
print(y)
[1, 2, 3, 4]
Great! You should now have an understanding of all the methods available for a list in Python!