From 442630f53709aa0bbae042e740731e754a132154 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 28 Jan 2010 15:14:16 -0500 Subject: [PATCH] clarify in vs. count vs. index --- native-datatypes.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native-datatypes.html b/native-datatypes.html index e008044..1393d9c 100755 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -337,8 +337,8 @@ ZeroDivisionError: Fraction(0, 0) ValueError: list.index(x): x not in list
  1. As you might expect, the count() method returns the number of occurrences of a specific value in a list. -
  2. If all you want to know is whether a value is in the list or not, the in operator is slightly faster than using the count() method. The in operator always returns True or False; it will not tell you where in the list the value is. -
  3. If you need to know exactly where in the list a value is, call the index() 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. +
  4. If all you want to know is whether a value is in the list or not, the in operator is slightly faster than using the count() method. The in operator always returns True or False; it will not tell you how many times the value appears in the list. +
  5. Neither the in operator nor the count() method will tell you where in the list a value appears. If you need to know where in the list a value is, call the index() 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.
  6. The index() method finds the first occurrence of a value in the list. In this case, 'new' occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.
  7. As you might not expect, if the value is not found in the list, the index() method will raise an exception.