why in returns values for array and keys for dictionary

Asun Friere afriere at yahoo.co.uk
Tue Aug 26 08:01:54 CEST 2008


On Aug 26, 10:49 am, "++imanshu" <himanshu.g... at gmail.com> wrote:
> Hi,
>
>     Wouldn't it be nicer to have 'in' return values (or keys) for both
> arrays and dictionaries.
>

NO!

When you iterate over a list (or even a array) it is the members of
the list in the order they appear that is of interest.  When you
iterate over a dictionary it is the relationship between the (unique)
key and the (possibly non-unique) value that is of interest.  Moreover
the sequence of values in a dictionary lacks meaning.

What is the 'key' of a list?  It's index?  It would be cumbersome to
iterate over the range(len(<list>)) and then have to use the index
values to pull out the values from that list.  On the otherhand it
would be useless for 'in' (in the sense of for x in {...}) to return a
series of unordered values, with no way to get at the key, rather than
keys (from which the values are directly accessible).

And what would you like file_like objects, for example, to return?



More information about the Python-list mailing list