0

We can access the value of a certain index in a list like so:

letters = ['a','b','c','d','e','f']
print letters[1]

The code above will print the letter 'b', because it is in the index 1 of the list, letter. So in this way, we can access a certain object in a list.

Now, if we assume that letters = ['a','b','c','d','e','f'], what code should we type to access the index of a certain object (assume it is the letter, 'c') in the list, letters?

Any and all help will be appreciated.

Right leg
  • 16,080
  • 7
  • 48
  • 81

1 Answers1

1

If letters = ['a', 'b', 'c', 'd', 'e', 'f'], the following will return the index of c in letters:

>>> letters.index('c')
2

If the value is not in the list, it will raise an error instead:

>>> letters.index('k')
valueError: 'k' is not in list
Right leg
  • 16,080
  • 7
  • 48
  • 81