Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why Lists do not have a find method?

I was trying to write an answer to this question and was quite surprised to find out that there is no find method for lists, lists have only the index method (strings have find and index).

Can anyone tell me the rationale behind that? Why strings have both?

like image 235
ChessMaster Avatar asked Oct 03 '10 07:10

ChessMaster


People also ask

Does list have a find method in Python?

Find an element using the list index() method. To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.

Does list have get method?

The get() method of List interface in Java is used to get the element present in this list at a given specific index. Syntax : E get(int index) Where, E is the type of element maintained by this List container.

How do I find a word in a list Python?

We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1. count(s) if count > 0: print(f'{s} is present in the list for {count} times.

How do you find the number in a list Python?

The most straightforward way to get the number of elements in a list is to use the Python built-in function len() . As the name function suggests, len() returns the length of the list, regardless of the types of elements in it.


1 Answers

I don't know why or maybe is buried in some PEP somewhere, but i do know 2 very basic "find" method for lists, and they are array.index() and the in operator. You can always make use of these 2 to find your items. (Also, re module, etc)

like image 169
ghostdog74 Avatar answered Sep 23 '22 18:09

ghostdog74