Problem with Lists & Index command

Johann Hibschman johann at physics.berkeley.edu
Fri Jun 11 17:47:25 EDT 1999


Benjamin Schollnick writes:

> It appears that INDEX doesn't work on a multiple
> dimension list????

Yes, index won't work.

Why?  Because the list is a list of tuples,

  lst = [("key1", "val1"), ("key2", "val2")...]

so "key1" isn't an element of the list.  The tuple ("key1", "val1")
is, however, but to find that you'd need to know the value, which
defeats the purpose.

Basically, the list could have been

   lst = [("key1", "val1"), ("key2", "val2"), "key1"]

Here, it seems much more plausible that lst.index("key1") will return
2, not 0.  Since list can contain any type, it doesn't automatically
know that it should look inside sublists to find elements.

-- 
Johann Hibschman                           johann at physics.berkeley.edu




More information about the Python-list mailing list