+2 votes
2 views
in Python by (4.1k points)
edited by

How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list?

2 Answers

0 votes
by (46.1k points)
edited by

You can find many methods to perform this task, let me help you with a few:

  • You can use the help function (useful for beginners) 

       >>> help(["foo", "bar", "baz"])

Help on list object:

class list(object)
...

 |
|  index(...)
|      L.index(value, [start, [stop]]) -> integer -- return first index of value
|

This method is quite useful and lead to the method you are looking for.

  •  You can use enumerate()

  indexes = [i for i,y in enumerate(ys) if y == 'asd'] 

 If you have any doubts, put them down below in the comments, cheers. 

0 votes
by (107k points)

You should use the interactive help function:

>>> help(["foo", "bar", "baz"])

Help on list object:

class list(object)

 ...

 |

 |  index(...)

 |      L.index(value, [start, [stop]]) ->       integer -- return first index of value

 |

which will often lead you to the method you are looking for.

You can use the following video tutorials to clear all your doubts:-

Related questions

+3 votes
2 answers
+3 votes
2 answers
+2 votes
3 answers
+1 vote
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...