Skip to content

Commit 442630f

Browse files
author
Mark Pilgrim
committed
clarify in vs. count vs. index
1 parent 825254b commit 442630f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

native-datatypes.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ <h3 id=searchinglists>Searching For Values In A List</h3>
337337
ValueError: list.index(x): x not in list</samp></pre>
338338
<ol>
339339
<li>As you might expect, the <code>count()</code> method returns the number of occurrences of a specific value in a list.
340-
<li>If all you want to know is whether a value is in the list or not, the <code>in</code> operator is slightly faster than using the <code>count()</code> method. The <code>in</code> operator always returns <code>True</code> or <code>False</code>; it will not tell you where in the list the value is.
341-
<li>If you need to know exactly where in the list a value is, call the <code>index()</code> method. By default it will search the entire list, although you can specify a second argument of the (0-based) index to start from, and even a third argument of the (0-based) index to stop searching.
340+
<li>If all you want to know is whether a value is in the list or not, the <code>in</code> operator is slightly faster than using the <code>count()</code> method. The <code>in</code> operator always returns <code>True</code> or <code>False</code>; it will not tell you how many times the value appears in the list.
341+
<li>Neither the <code>in</code> operator nor the <code>count()</code> method will tell you <em>where</em> in the list a value appears. If you need to know where in the list a value is, call the <code>index()</code> method. By default it will search the entire list, although you can specify an optional second argument of the (0-based) index to start from, and even an optional third argument of the (0-based) index to stop searching.
342342
<li>The <code>index()</code> method finds the <em>first</em> occurrence of a value in the list. In this case, <code>'new'</code> occurs twice in the list, in <code>a_list[2]</code> and <code>a_list[4]</code>, but the <code>index()</code> method will return only the index of the first occurrence.
343343
<li>As you might <em>not</em> expect, if the value is not found in the list, the <code>index()</code> method will raise an exception.
344344
</ol>

0 commit comments

Comments
 (0)