why in returns values for array and keys for dictionary

alex23 wuwei23 at gmail.com
Mon Aug 25 22:57:06 EDT 2008


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.

In both cases, 'in' returns a boolean indicating the existence of an
item in the list, or a key in the dict. I'm not sure why you'd need it
to return the item you're checking for the existence of, as you'd have
to have that item before you could do the check.

Have I missed what you're asking for here? Could you provide a
pseudocode example to demonstrate what you mean?



More information about the Python-list mailing list