Python hosting: Host, run, and code Python in the cloud!
Arrays are usually referred to as lists. For convience, lets call them arrays in this article.
Python has a method to search for an element in an array, known as index().
We can find an index using:
Arrays start with the index zero (0) in Python:
If you would run x.index(‘p’) you would get zero as output (first index).
Array duplicates: If the array contains duplicates, the index() method will only return the first element.
If you want multiple to find multiple occurrences of an element, use the lambda function below.
To find an element in a string array use:
You could use this code if you want to find multiple occurrences:
The index method works on numeric arrays too:
To find multiple occurrences you can use this lambda function: