May 2019
Beginner
528 pages
29h 51m
English
Often, you’ll want to determine whether a sequence (such as a list, tuple or string) contains a value that matches a particular key value. Searching is the process of locating a key.
index
List method index takes as an argument a search key—the value to locate in the list—then searches through the list from index 0 and returns the index of the first element that matches the search key:
In [1]: numbers = [3, 7, 1, 4, 2, 8, 5, 6]In [2]: numbers.index(5)Out[2]: 6
A ValueError occurs if the value you’re searching for is not in the list.
Using method index’s optional arguments, you can search a subset of a list’s elements. You can use *= to multiply a sequence ...
Read now
Unlock full access