Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-check what position it is in a list [duplicate]

Tags:

python

list

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.

like image 832
jcdenton Avatar asked Jul 14 '26 21:07

jcdenton


1 Answers

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]
like image 139
Mohammed Aouf Zouag Avatar answered Jul 16 '26 09:07

Mohammed Aouf Zouag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!