Built-in Functions for lists
In section 4.3 we introduced python’s built-in functions. We talked about a couple general functions like input()
, max()
, and abs()
as well as some string specific functions like str.islower()
, str.capitalize()
, and str.upper()
.
Now we are going to go over two types of built-in functions dealing with lists:
- built-in functions that take lists as parameters.
- list functions that are built-in. (Called similarly to string functions.)
Built-in functions that take lists as parameters
Some of the standard python built-in functions can take lists as parameters. Here is a list of some of the most useful ones for this course.
max(list of values)
: notice that the first definition of the max
function reads “max(iterable, *[, key, default])”. This means that the max
function can take any type that can be iterated over (as with a for loop) as a parameter. The “*” indicates that the rest of the parameters are optional.
# anything that we can put in the blank for the statement "for variable in ________" can be given to the
# max function
print(max(range(2, 5)))
print(max("The quick brown fox jumped over the lazy dog."))
print([-4.3, -7, -12, -0.3])
The same goes for the min
function. We also now get access to some fun new functions:
sum(iterable)
: sums all values in the given iterable (like a list) and returns the resulting value.
sorted(iterable)
: sorts the values in the given iterable and returns a sorted list of those values.
# TODO: do some experiments with the sum and sorted functions!
# does sum work on strings? does sorted work on strings?
# does sum work on lists? does sorted work on lists?
# does sum work on the range function? does sorted work on the range function?
Our last extremely useful function that takes a list as a parameter that we’ll talk about in this section of notes is a string function called join
.
str.join(iterable)
: join the elements in the given iterable using str
as glue inbetween the elements. Returns one single string. Elements in the iterable must be strings.
This method can be thought of as the opposite of the str.split()
function. Instead of going from 1 string to a list of strings, it takes a list of strings and produces 1 string.
print("glue".join(["here", "are", "words"]))
print(" ".join(["here", "are", "words"]))
# TODO: what happens if you try to use join on a list that contains elements that are not strings?
List functions that are built-in
Similarly to strings, lists come with a number of built-in functions. So far, we’ve only talked about list.append()
and del list[index]
.
There are a number of list functions here. We’re going to concentrate on a couple of them:
list.copy()
: return a copy of this list. Can also be written as list[:]
.
l = [2, 5, 6, 12]
k = l.copy()
#k = l[:] # this line is equivalent to the line above it
print(l)
print(k)
k[0] += 10
print(l)
print(k)
list.count(value)
: return the count of the occurrences of value
in list
. Same as the string version of this function.
list.index(value, [start index, [end index]])
: return the index that value
occurs at in this list. You may optionally pass in a start index
and an end index
indicating what part of the list the search should occur at. Same as the string version of this function.
list.sort()
: sort the items of the list in place. This is a mutator function. It does not return a value
# TODO: experiment with the above functions here!