Index

Indexes count up from 0 when you start on the lefthand side. In the string "hello", the "h" is at index 0, the "e" is at 1, the first "l" is at 2, and so on.

Indexes count down from -1 if you start on the righthand side. Using the string "hello", we could get the "o" at the index -1, the second "l" at -2, etc.

Both strings and lists have a .index() method that will give you the index for a particular value. Using our old friend "hello", if we wanted to find the index for the "e", we could do "hello".index("e").

.index() only gives the index for the first occurrence of whatever you searched for.

0:00

Strings and lists are both iterable,

0:02

which means that they hold on
to more than one thing in them.

0:05

It also means that we
can loop through them.

0:07

But we're not ready for loops just yet.

0:10

But these two data types have
something else in common that's very

0:13

important to learn about.

0:15

If you've used other programming languages
you'll probably know what I mean

0:19

when I say they have indexes.

0:21

If you haven't used other languages,

0:23

well that word might or
might not mean anything.

0:27

Strings and
lists have multiple things in them.

0:30

Strings have multiple characters and
lists have multiple members.

0:33

These characters and
members always have a definite order.

0:36

Something comes first,
and something else second,

0:38

and something else third, and so on.

0:41

We call the space where
something is its index.

0:44

Unlike you, though, Python says
the first item has the index of 0 and

0:48

the second one has the index of 1.

0:51

Item three is at index 2 and so on.

0:54

We can also index things from
the end by using negative numbers.

0:58

But the last item is -1 instead of -0.

1:01

The next to last item is -2 and
all the way back to the first item.

1:07

Indexes are simple, just giving numbers
to positions in a list or string.

1:11

But, they're often confusing to newcomers.

1:13

So, I think a trip to
Workspaces is in order.

1:16

Like I said, the first item in a string or
a list has the index of 0.

1:21

Let's make a string to play with,
so abcde.

1:26

Yeah, we don't need that.

1:27

We'll just put five letters.

1:29

And so if I was to do alpha.index, which
uses the index method of the string class,

1:35

and I give it the string a,
it says that that's at index 0.

1:39

The index method returns
the index of the first instance

1:43

of whatever it is that we're looking for.

1:46

Now that means that we
can't really rely on it

1:50

if our string might have
multiple items in it.

1:53

It won't tell us about later instances.

1:55

I will never get index
to tell me about this a.

1:59

Because it hits this one, and goes cool
there's your a that you wanted and

2:02

then immediately returns.

2:05

The index method exists for lists,
too, and it works in the same way.

2:10

So we if did an alpha_list,
which we can just do a list of alpha, and

2:14

I did alpha_list.index of the string b, it
tells me it's at 1, it's the one-th index,

2:21

which that's a really fun word to spring
on people who aren't programmers.

2:25

And so there we go.

2:27

It's one-th.
Now it also works for groups of items.

2:31

So we can do alpha.index('cd'), and

2:36

I get that it starts at 2,
which we can see,

2:41

we've got 0, 1, and 2.

2:45

So what if we want to search for and index
of something that doesn't exist then?

2:50

Let's do alpha.index for ce,

2:53

which doesn't appear anywhere in
our string, we get a value error.

2:57

The substring wasn't found.

3:00

Which is true, the string here, ce,
wasn't ever found in our actual string.

3:06

But it's another thing that
you have to watch out for,

3:09

you gotta make sure that your index works.

3:12

So we have the index of something.

3:14

How do we get the something
that's at that index?

3:17

Well we use the index,

3:19

which is always going to be an integer
in between square brackets.

3:23

So we do alpha[0],
we get back the letter a.

3:27

We do alpha_list[2] and we get back the c.

3:31

Now you don't have to use the index
method to find the index of course.

3:37

You can just count them yourself.

3:38

If we were to print alpha,
we can just count these things out.

3:44

So we start at zero.

3:45

a is 0, b is 1, c is 2,
d is 3, and e is 4.

3:53

And knowing that Python lets
us use negative indexes,

3:59

we can also count that e is -1,

4:02

d is -2, c is -3, b is -4, and a is -5.

4:06

So we can find an item's index
with the index method, or

4:11

by counting them out ourselves.

4:13

And we can get items from a list or

4:15

a string by its index
using the square brackets.

4:18

Indexes are in pretty much
every programming language, but

4:21

they don't always work the same.

4:23

Most of the time though, you'll find
yourself using three indexes, 0, 1,

4:28

and -1.

4:28

We have one more handy feature about
indexes that we need to talk about, so