why in returns values for array and keys for dictionary

Dan Bishop danb_83 at yahoo.com
Tue Aug 26 00:12:31 EDT 2008


On Aug 25, 9:57 pm, alex23 <wuwe... at gmail.com> wrote:
> On Aug 26, 10:49 am, "++imanshu" <himanshu.g... at gmail.com> wrote:
>
> >     Wouldn't it be nicer to have 'in' return values (or keys) for both
> > arrays and dictionaries. Arrays and Dictionaries looked so similar in
> > Python until I learned this difference.
>
> By 'arrays' do you mean lists? tuples?
>
> I'm not sure how you'd ever find lists & dictionaries similar...
>
> >>> alist
> [1, 2, 3]
> >>> adict
>
> {'a': 1, 'c': 3, 'b': 2}
>
> One is a sequence, with convenience functions for treating it like a
> stack or a queue.
> The other is a mapping between keys and pairs.

You could argue that lists are also a mapping between keys and pairs,
with the constraint that the keys have to be the integers from 0 to
len(x)-1.  That is, ['a', 'b', 'c'] is like {0: 'a', 1: 'b', 2: 'c'},
at least as far as the [] operator and the len function are concerned.



More information about the Python-list mailing list