3
list1 = ['physics', 'chemistry', "dog", "cat","dog"];

I want it to say if I typed in an input dog it will tell me dog is the forth one and sixth one.

1
  • I have swapped the dupe target to a more suited question. Commented Feb 1, 2016 at 18:27

1 Answer 1

2

Use list comprehensions:

list1 = ['physics', 'chemistry', "dog", "cat","dog"]

string = input()
res = [i for i, el in enumerate(list1) if el == string]

print(res)

Output:

[2, 4]
Sign up to request clarification or add additional context in comments.

3 Comments

thanks you just saved my course work like 20 mins of experimenting
for reference how does this work so i can use any information i scrounge for future reference
@jcdenton check this docs.python.org/2/tutorial/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.