why in returns values for array and keys for dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ++imanshu

    why in returns values for array and keys for dictionary

    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
  • alex23

    #2
    Re: why in returns values for array and keys for dictionary

    On Aug 26, 10:49 am, "++imanshu" <himanshu.g...@ gmail.comwrote:
        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?

    Comment

    • alex23

      #3
      Re: why in returns values for array and keys for dictionary

      On Aug 26, 12:57 pm, alex23 <wuwe...@gmail. comwrote:
      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.

      Comment

      • Dan Bishop

        #4
        Re: why in returns values for array and keys for dictionary

        On Aug 25, 9:57 pm, alex23 <wuwe...@gmail. comwrote:
        On Aug 26, 10:49 am, "++imanshu" <himanshu.g...@ gmail.comwrote:
        >
            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.

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: why in returns values for array and keys for dictionary

          On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote:
          On Aug 26, 10:49 am, "++imanshu" <himanshu.g...@ gmail.comwrote:
          >    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

          Comment

          • Erik Max Francis

            #6
            Re: why in returns values for array and keys for dictionary

            Marc 'BlackJack' Rintsch wrote:
            On Mon, 25 Aug 2008 19:57:06 -0700, alex23 wrote:
            >
            >On Aug 26, 10:49 am, "++imanshu" <himanshu.g...@ gmail.comwrote:
            >> 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.
            That is basically the same question. Iterating over a list gives you
            its elements, and using the `in` operator with lists tells you whether
            or not an object is an element of the list. Iterating over a dictionary
            gives you its _keys_, not its values, and the `in` operator with
            dictionaries tells you whether or not a _key_ is in the dictionary.
            >>l = [1, 2, 3]
            >>for x in l: print x
            ....
            1
            2
            3
            >>0 in l
            False
            >>1 in l
            True
            >>d = {'a': 1, 'b': 2, 'c': 3}
            >>for x in d: print x
            ....
            a
            c
            b
            >>'a' in d
            True
            >>1 in d
            False

            The reason why people find it more useful to deal with keys rather than
            values of a dictionary during iteration or containment testing is ...
            because that tends to be what you're usually more interested in, and is
            more efficient. For another thing, if you're doing a lot of testing for
            containment in values, then it's likely you're not using the right data
            structure, or combination of data structures. That's not what
            dictionaries are for.

            --
            Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
            Well I have been puppetized / Oh how I have compromised
            -- Lamya

            Comment

            • Erik Max Francis

              #7
              Re: why in returns values for array and keys for dictionary

              ++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.
              It's because dealing with keys makes far more sense, since that's how
              the dictionary data structure works. If you're doing this an awful lot
              -- whether testing for inclusion or iterating -- then you're probably
              using the wrong data structure.

              --
              Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
              San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
              Well I have been puppetized / Oh how I have compromised
              -- Lamya

              Comment

              • ++imanshu

                #8
                Re: why in returns values for array and keys for dictionary

                On Aug 26, 11:52 am, Erik Max Francis <m...@alcyone.c omwrote:
                ++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.
                >
                It's because dealing with keys makes far more sense, since that's how
                the dictionary data structure works.  If you're doing this an awful lot
                -- whether testing for inclusion or iterating -- then you're probably
                using the wrong data structure.
                Thanks for the detailed replies. Python seems to be taking the
                pragmatic approach here instead of the pure one. And yes it makes more
                sense.

                Thanks,
                ++imanshu
                >
                --
                Erik Max Francis && m...@alcyone.co m &&http://www.alcyone.com/max/
                  San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
                   Well I have been puppetized / Oh how I have compromised
                    -- Lamya

                Comment

                • bearophileHUGS@lycos.com

                  #9
                  Re: why in returns values for array and keys for dictionary

                  "++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

                  Comment

                  • Lie

                    #10
                    Re: why in returns values for array and keys for dictionary

                    On Aug 26, 4:04 pm, "++imanshu" <himanshu.g...@ gmail.comwrote:
                    On Aug 26, 11:52 am, Erik Max Francis <m...@alcyone.c omwrote:
                    >
                    ++imanshu wrote:
                        Wouldn't it be nicer to have 'in' return values (or keys) forboth
                    arrays and dictionaries. Arrays and Dictionaries looked so similar in
                    Python until I learned this difference.
                    >
                    It's because dealing with keys makes far more sense, since that's how
                    the dictionary data structure works.  If you're doing this an awful lot
                    -- whether testing for inclusion or iterating -- then you're probably
                    using the wrong data structure.
                    >
                    Thanks for the detailed replies. Python seems to be taking the
                    pragmatic approach here instead of the pure one. And yes it makes more
                    sense.
                    Anyway, there is two obvious choice when dealing with dictionary
                    looping: return the keys and return the key and value. The python
                    designer thought that it is trivial to get the value from key so
                    returning both key and value, when the value might not be used at all,
                    is redundant and might cause a slow down. In the case where it is
                    actually needed for practicality (usually one-liner), then
                    dict.iteritems( ) is provided, in other cases where you don't need the
                    key (usually when you're applying a uniform function to dictionary or
                    to convert it to a list), dict.itervalues () is provided, and for
                    completeness dict.iterkeys() which is completely useless in a for-loop
                    is provided (the only case where it _might_ be useful is in
                    callbacks).

                    Anyway, in short (in code):
                    for key in dict:
                    print key, dict[key]

                    is exactly the same as:
                    for key, value in dict.iteritems( ):
                    print key, value

                    or
                    for key in dict:
                    value = dict[key]
                    print key, value
                    # the next line is not needed if you don't modify an immutable
                    value
                    dict[key] = value

                    Comment

                    • Terry Reedy

                      #11
                      Re: why in returns values for array and keys for dictionary



                      Lie wrote:
                      Anyway, there is two obvious choice when dealing with dictionary
                      looping: return the keys and return the key and value.
                      The python designer thought...
                      The issue of whether there should be a default iterator and if so, which
                      of the two obvious choices should be picked, was discussed on the py-dev
                      list by several developers in addition to GvR. 'Yes, iterkeys' was the
                      majority choice as the most useful for the most users.

                      The rationale for no default would have been 'explicit is better than
                      implicit' and the easy prediction (proven true ;-) that either choice
                      would be challenged. Of course, no default would have been criticized
                      also.

                      tjr

                      Comment

                      Working...