πIterable Functions
What can you do with lists?
Now that we've developed some familiarity with lists, let's quickly discuss some functions that can be used with them to change them or to retrieve values from lists!
Append
To add an element to the list, we use the append keyword. It is essential to remember that append is a mutating function βΒ it returns None. Don't use its return value for anything!
>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]What happens when you try to append a list to a list?
>>> lst = [1, 2, 3]
>>> lst.append([4, 5])
>>> lst
[1, 2, 3, [4, 5]]As we hopefully expected, [4, 5] is considered one element and appended to the list as such!
Extend
Contrary to append, extend accepts an iterable and appends all the elements of that iterable to the list. Extend is also a mutating function and returns None.
>>> lst = [1, 2, 3]
>>> lst.extend([4, 5])
>>> lst
[1, 2, 3, 4, 5]What happens when we try to extend the list with something that is not an iterable?
>>> lst = [1, 2, 3]
>>> lst.extend(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterableWhat happens when we try to extend the list with a list that contains more lists?
>>> lst = [1, 2, 3]
>>> lst.extend([4, [5, 6]])
>>> lst
[1, 2, 3, 4, [5, 6]]Did you figure out that one? It's absolutely alright if you didn't! Hopefully this example helped you develop the intuition behind how extend appends every element of the iterable it passes in. Also important to remember is that the iterable doesn't need to be a list!
>>> lst = [1, 2, 3]
>>> lst.extend(range(5))
>>> lst
[1, 2, 3, 0, 1, 2, 3, 4]Sum
Unlike append and extend, which are mutating functions, sum is our first aggregating function. It accepts a list and a starting value, and returns the sum of all the elements of the list and the starting value.
>>> lst = [1, 2, 3]
>>> sum(lst, 10)
16Note that the starting value is optional.
>>> lst = [1, 2, 3]
>>> sum(lst)
6What happens when the list is made up of different types of elements? For example:
>>> lst = [1, 2, 's']
>>> sum(lst)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'Therefore, we have to be careful using the sum function, lest we run into unexpected errors like the one above!
Any
any accepts a list of elements and returns True if any of the elements are truth-y (that is, evaluate to true) and False otherwise. For more about truth-y and false-y values, read here.
>>> lst = [True, False, False]
>>> any(lst)
True
>>> lst = [False, False, False]
>>> any(lst)
False
>>> lst = [2, 3, 0]
>>> any(lst)
True
>>> lst = [0, None, ""]
>> any(lst)
FalseKeep in mind any function's behavior with this edge case:
>>> lst = []
>>> any(lst)
FalseAll
all is the complement to any. all accepts a list of elements returns True if every element evaluates to True, and False otherwise.
>>> lst = [True, True, False]
>>> all(lst)
False
>>> lst = [True, True, True]
>>> all(lst)
True
>>> lst = [2, 3, 0]
>>> all(lst)
False
>>> lst = [2, 3, 4]
>>> all(lst)
TrueKeep in mind all function's behavior with this edge case:
>>> lst = []
>>> all(lst)
Truemax
max accepts a list of values, and given this list, returns the maximum value of that list.
>>> max([1, 2, 3])
3
>>> max([1, 2, -1])
2
>>> max([1, 2, 's'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'Another error to keep in mind is the following:
>>> max([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequenceThat is to say, max cannot be given an empty list.
The min function works exactly like the max function, with the same error cases, with the critical difference of course being that it returns the minimum value instead of the maximum one.
Last updated
Was this helpful?