0

I am trying to get indexes for adams and lewis from a list.

Presidents = ['washington','adams','jeff','lewis']

def index():
    for index,name in enumerate(Presidents):
        if name == 'adams':
            return index

print(index())

How can I modify this function? So that when the function index() is called, it should return the index of adams and lewis as integers.

dspencer
  • 4,297
  • 4
  • 22
  • 43
  • 2
    You can do `Presidents.index('adams')`, `Presidents.index('lewis')`. If you like'd to get indices as a list, you can use `[Presidents.index(name) for name in ['adams', 'lewis']]` – CodeIt Apr 08 '20 at 14:50
  • the index are 1 and 3. – raven Apr 08 '20 at 14:52
  • Does this answer your question? [Finding the index of an item in a list](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list) – CodeIt Apr 08 '20 at 14:55

1 Answers1

-1

You can just reuse the index method.

The method returns the index of the element in the list.

If not found, it raises a ValueError exception indicating the element is not in the list.

def index(name):
  return Presidents.index(name)


print(index('adams'))
print(index('lewis'))
raven
  • 2,381
  • 2
  • 20
  • 44