s.index(x[, i[, j]]) will change the s ?

Chris Rebert clp2 at rebertia.com
Thu Sep 10 06:33:04 CEST 2009

On Wed, Sep 9, 2009 at 9:00 PM, s7v7nislands<s7v7nislands at gmail.com> wrote:
> hi all:
>    what is the s.index() mean? does the index() change the s?

It tells you the index of the first instance of the given element in
the sequence. Or, to quote the docs:
    s.index(x[, i[, j]]) --- return smallest k such that s[k] == x and
i <= k < j

No, .index() does not modify the sequence itself.

>    In python2.6 doc (6.6.4. Mutable Sequence Types), Note 4:
> Raises ValueError when x is not found in s. When a negative index is
> passed as the second or third parameter to the index() method, the
> list length is added, as for slice indices. If it is still negative,
> it is truncated to zero, as for slice indices.
> Changed in version 2.3: Previously, index() didn’t have arguments for
> specifying start and stop positions.

Nothing in the above says anything about modifying a sequence...

> who can give a example?  and why the s.remove() also point to note 4?

Because it has the same behavior when the item is not present in the sequence.

Examples using lists:

assert ["c", "a", "b", "c", "c"].index("c", 1) == 3

    ["a", "b"].index("c")
except ValueError:
    print "'c' was not in the list"
    raise RuntimeError, "Should never get here"

x = ["a", "b", "c"]
assert len(x) == 2 and x[0] == "a" and x[1] == "c"

> Is the document wrong?

No. What made you think so?


More information about the Python-list mailing list