why in returns values for array and keys for dictionary

Discussion in 'Python' started by ++imanshu, Aug 26, 2008.

  1. ++imanshu

    ++imanshu Guest

    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
     
    ++imanshu, Aug 26, 2008
    #1
    1. Advertising

  2. ++imanshu

    alex23 Guest

    On Aug 26, 10:49 am, "++imanshu" <> 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?
     
    alex23, Aug 26, 2008
    #2
    1. Advertising

  3. ++imanshu

    alex23 Guest

    On Aug 26, 12:57 pm, alex23 <> 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.
     
    alex23, Aug 26, 2008
    #3
  4. ++imanshu

    Dan Bishop Guest

    On Aug 25, 9:57 pm, alex23 <> wrote:
    > On Aug 26, 10:49 am, "++imanshu" <> 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.
     
    Dan Bishop, Aug 26, 2008
    #4
  5. On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote:

    > On Aug 26, 10:49 am, "++imanshu" <> 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
     
    Marc 'BlackJack' Rintsch, Aug 26, 2008
    #5
  6. ++imanshu

    alex23 Guest

    On Aug 26, 2:30 pm, Marc 'BlackJack' Rintsch <> 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 :)
     
    alex23, Aug 26, 2008
    #6
  7. ++imanshu

    Asun Friere Guest

    On Aug 26, 10:49 am, "++imanshu" <> 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?
     
    Asun Friere, Aug 26, 2008
    #7
  8. ++imanshu

    Guest

    "++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
     
    , Aug 26, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish Sahani
    Replies:
    11
    Views:
    629
    Roberto Bonvallet
    Jun 7, 2006
  2. Replies:
    14
    Views:
    595
    Antoon Pardon
    Jul 4, 2006
  3. Delaney, Timothy (Tim)
    Replies:
    4
    Views:
    333
  4. Delaney, Timothy (Tim)
    Replies:
    2
    Views:
    317
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,265
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page