Common Functions and Methods in Lists
let's conclude our discussion on lists by discussing various functions associated with them.
Functions
As mentioned previously, a function is a piece of code that is called by name. It can be passed data to operate on and can optionally return data. Let's look at some examples of functions that can be applied to lists:
The len
function
len
simply returns the number of characters in a sequence.
numbers = [3, 5, 6, 7, 9] length_of_numbers = len( numbers ) print(length_of_numbers)
>> 5
The output as expected is 5
, since that's how many elements we have in the list numbers
.
The **max**
function
max will return the greatest element in a list
numbers = [3, 5, 6, 7, 9] maximum_number = max( numbers ) print(maximum_number)
>> 9
- What if we had a list of strings? how would
max
work then?
names = ["Thomas", "Gio", "Zack", "Adam"] print(max(names))
>> Zack
When dealing with max
in a list of strings, the maximum value would be the string that appears last based on alphabetical order.
We can conclude that max works differently depending on which data type you're using with it.
What if the list has a mix of strings and numbers?
let's inject a number in our list:
names = ["Thomas", "Gio", "Zack", "Adam", 1] print(max(names))
TypeError: '>' not supported between instances of 'int' and 'str'
You get an error. The data types are incompatible.
The min
function
Let's try the min
function, and as you might have guessed, the min
function will do the exact opposite of the max
function.
names = ["Thomas", "Gio", "Zack", "Adam", 1] print(max(names))
>> Gio
It will return the string that shows up first, based on alphabetical order.
Intuitively, for integers, min
would return the smallest value:
numbers = [3, 5, 6, 7, 9] minimum_number = min( numbers ) print(minimum_number)
>> 3
The sorted
function
Another useful function that we can use for lists is sorted
. sorted
will take in a list as an argument and sort it from smallest to largest :
numbers = [3, 5, 6, 7, 9] sorted_number = sorted( numbers ) print(sorted_number)
>> [3, 5, 6, 7, 9]
we can do the same thing to our names
list:
names = ["Thomas", "Gio", "Zack", "Adam"] sorted_names = sorted(names) print(sorted_names)
>> ['Adam', 'Gio', 'Thomas', 'Zack']
sorted will sort a list of words alphabetically.
Methods
As mentioned previously, a method is a function associated with an object. Let's look at some common methods in lists:
The join
method
The join
method is actually a string method that takes in as an argument a list of strings. What it's going to do is return a string consisting of every element on that list joined by the string.
joined_string = '-'.join(['january', 'february', 'march']) print(joined_string)
>> january-february-march
Instead of a hyphen -
, you could also do a blank space.
joined_string = ' '.join(['january', 'february', 'march']) print(joined_string)
>> january february march
The format
method
format
replaces curly brackets { }
in a string with any other string value:
formatted_string = "this person is {}, {}, and {}".format('tall', 'slim', 'blonde') print(formatted_string)
>> this person is tall, slim, and blonde
The append
method
Another useful method is the append
method. As the name suggests, you can use this method to append any value to a list.
months = ['January', 'February', 'March'] months.append('April') print(months)
>> ['January', 'February', 'March', 'April']
This concludes our discussion on lists. In the next article, we will have a look at tuples.