🌋
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!
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! 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 iterable
What 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]
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)
16
Note that the starting value is optional.
>>> lst = [1, 2, 3]
>>> sum(lst)
6
What 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
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)
False
Keep in mind
any
function's behavior with this edge case:>>> lst = []
>>> any(lst)
False
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)
True
Keep in mind
all
function's behavior with this edge case:>>> lst = []
>>> all(lst)
True
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 sequence
That 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 modified 1yr ago