Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > why in returns values for array and keys for dictionary

Reply
Thread Tools

why in returns values for array and keys for dictionary

 
 
++imanshu
Guest
Posts: n/a
 
      08-26-2008
Hi,

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.

Thanks,
++imanshu
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 10:49*am, "++imanshu" <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 12:57*pm, alex23 <(E-Mail Removed)> wrote:
> By 'arrays' do you mean lists? tuples?


My apologies, there actually -is- an array type in Python.

I've just honestly never had any cause to use it

I'm still not entirely sure what you would like 'in' to do, though.

 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      08-26-2008
On Aug 25, 9:57*pm, alex23 <(E-Mail Removed)> wrote:
> On Aug 26, 10:49*am, "++imanshu" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-26-2008
On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote:

> On Aug 26, 10:49*am, "++imanshu" <(E-Mail Removed)> 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.

>
> […]
>
> 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?


The OP isn't talking about the ``in`` operator but ``in`` as part of
``for … in …``. So it's actually the question why ``list(a_dict)``
doesn't return a list of values but a list of keys.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 2:30*pm, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> The OP isn't talking about the ``in`` operator but ``in`` as part of
> ``for in ``. *So it's actually the question why ``list(a_dict)``
> doesn't return a list of values but a list of keys.


Aaaah! Cheers, Marc, that didn't occur to me.

imanshu: my apologies for the confusion in my original replies, I now
understand what you mean.

I'd hazard to suggest the answer is one of practicality over purity
between object types.

I can actually see there being a perceived advantage in providing a
consistent interface, so that:

for index,value in array('i',[0,1,2]): ...
for index,value in [0,1,2]: ...
for key,value in {'0':0,'1':1,'2':2}: ...

would all produce equivalent results. I'd guess that what is provided
now meets the most common use cases, with convenience functions
readily available for the next most common, ie via enumerate() for
arrays & lists, and .itervalues() or .iteritems() for dicts.

It all comes down to the intent of the data types used. I'm -
generally- more concerned with the values of a list than knowing their
position, especially when the position is implicit in the order
returned. When working with dicts, if I need to modify their contents
I -need- to know the key, as the key isn't an implicit aspect of the
return order of the values, and is the only way distinguish between
identical values.

I'm -still- embarrassed that I missed the existence of the array type,
however
 
Reply With Quote
 
Asun Friere
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 10:49 am, "++imanshu" <(E-Mail Removed)> 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?
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-26-2008
"++imanshu:
> 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.


D language works like you say, and it's awful. With a key you can find
its value, but given only the value you can't find its key.

Bye,
bearophile
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Dictionary .keys() and .values() should return a set [with Python 3000 in mind] vatamane@gmail.com Python 14 07-04-2006 07:36 AM
RE: Dictionary .keys() and .values() should return a set[withPython3000 in mind] Delaney, Timothy (Tim) Python 2 07-03-2006 09:49 AM
RE: Dictionary .keys() and .values() should return a set [withPython3000 in mind] Delaney, Timothy (Tim) Python 4 07-03-2006 08:57 AM
Concatenating dictionary values and keys, and further operations Girish Sahani Python 11 06-07-2006 04:22 PM



Advertisments