Exploring Other List Features
00:00
As a sequence type, there are still many more operations available to lists. The most useful of these will include operators in
and not in
, the list methods .count()
and .index()
, the built-in functions, len()
, min()
and max()
, and the comparators or comparison operators less than, greater than, and equals.
00:19 Let’s run through some examples of these in yes, you guessed it, the REPL.
00:24
We’ll start with a list of integers called numbers
that will also contain a few duplicates and at least one negative: numbers = [12, -9, 10, 12, 12, 50]
.
00:38
You can use the in
operator to check whether an item is present in a list. Try it with the integer 13
: 13 in numbers
returns False
.
00:48
Conversely, using not in
, you can check whether 13
is not present in the list.
00:54
13 not in numbers
returns True
. And if you check whether the integer 10
is in numbers
, 10 in numbers
also returns True
.
01:04
Okay. So what about the .index()
method? When you pass a value to the .index()
method, it searches the list for that value and returns the first index it finds.
01:13
If the value isn’t found, it raises an error. Try calling numbers.index()
and passing in 10
.
01:20
And it returns 2
because 10
is the third element of the list. And how does this handle an element with duplicates? numbers.index(12)
. In this case, Python returns the first instance and only that one.
01:34
Because 12
is the first element in the list, you get the index 0
. And if you use .index()
with an element that isn’t in the list, numbers.index(13)
01:47
returns a ValueError
because 13
is not in the list. So it’s best to use in
and not in
to check for inclusion, not the .index()
method.
01:56
Next up, the .count()
method. This should return the number of times an element appears in a list. Try it with our lucky number 13
: numbers.count(13)
02:07
and it returns 0
because there are zero thirteens in the list. And thankfully, this didn’t raise an error. And now if you try numbers.count(12)
02:19
as you may have expected, the result is 3
because there are three twelves in this list. What if you want to know how many elements are in a list?
02:28
You can use the built-in len()
function. Call len()
passing in numbers
and the result is 6
. Yep. Because there are six elements in numbers
.
02:38
You can get the maximum value or the largest number using the built-in max()
function. Call max()
passing in numbers
and it returns 50
.
02:48
On the other hand, the minimum value can be found using min()
. Call min()
passing in numbers
and this returns -9
the lowest number. Let me just clear the screen.
03:03 And now you can look at some comparisons. Lists are compared using what’s known as lexicographical ordering. And that’s a fancy way to say that first, the first elements of the list are compared.
03:14
If they’re different, there’s your result. But if they’re the same, then the second elements are compared, and so on. So if you compare the list [2, 3]
with the list [2, 3]
using the equality operator or double equal sign: [2, 3] == [2, 3]
03:33
the result is True
. If you compare the same two lists with the inequality operator, [2, 3]
!= [2, 3]
, the result is, of course, False
. How about this?
03:48
The list [2, 3]
is less than the list [3, 2]
.
03:54
And that’s True
because 2
, the first element of the left-hand list, is less than 3
the first element of the right-hand list.
04:01
Here’s another one, using the greater than operator this time. The list [2, 3, 4]
greater than the list [3, 2]
. And this returns False
because 2
is still less than 3
regardless of the first list being longer than the second.
04:17
You can combine the less than and greater than operators with the equals operator. These operators, the greater than or equals and less than or equals operators, will always return True
if the operands are equal. The list [3, 4]
is less than or equal to the list [3, 4]
returns True
.
04:36
And the list [3, 4]
greater than or equal to the list [3, 4]
also returns True
. But for true equality to hold, every element in the two lists must be equal.
04:48
The list [3, 4, 2]
double equals the list [3, 4]
returns False
. They’re not truly equal.
04:57 Alright. And I guess that’s all there is to say about lists. Wait, wait, hold on. They’re telling me, they’re telling me there’s more. A warning? Something about common mistakes to avoid.
05:07 Looks like our work here isn’t done yet. Next up, when and how not to use lists.
Become a Member to join the conversation.