newbie question - iterating through dictionary object

Discussion in 'Python' started by mirandacascade@yahoo.com, Feb 17, 2005.

  1. Guest

    I am attempting to understand the difference between two techniques
    that use a for/in loop to iterate through entries in a dictionary
    object. Copy/paste of interactive window illustrates.

    >>> a = {}
    >>> a.update({1:'a'})
    >>> a.update({2:'b'})
    >>> a

    {1: 'a', 2: 'b'}
    >>> for x in a:

    .... print x
    .... print a[x]
    ....
    1
    a
    2
    b
    >>> y = a.keys()
    >>> for z in y:

    .... print z
    .... print a[z]
    ....
    1
    a
    2
    b

    The techniques appear to return the same results. In the first
    example, for each loop iteration, x appears to have the value of the
    key of the dictionary object. In the second example, z appears to have
    the value of the key in the dictionary object. Assuming that I want to
    iterate through each dictionary entry, and I don't care about the order
    in which the code iterates (i.e. I don't need to sort the list that
    gets returned to y when it is assigned the value of the expression
    a.keys()), my questions are:

    1) Is there any advantage to use the

    y = a.keys()
    for z in y:

    looping technique rather than the

    for x in a:

    looping technique?

    2) What are the tradeoffs for using each of the techniques?
     
    , Feb 17, 2005
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    >>>>> "Miranda" == mirandacascade <> writes:

    Miranda> 1) Is there any advantage to use the
    Miranda> y = a.keys()
    Miranda> for z in y:

    While you're at it, you could save y altogether and just use

    for z in a.keys():
    ...

    Miranda> looping technique rather than the
    Miranda> for x in a:
    Miranda> looping technique?

    I certainly don't see any advatages.

    Miranda> 2) What are the tradeoffs for using each of the techniques?

    'for x in a:' is prettier to look at. :) As you can see, it's also
    slightly faster (at least for very small dictionaries), but unless
    you're doing a _lot_ of them, it won't matter.

    % python2.4 -m timeit -c "a = {1:1, 2:2, 3:3, 4:4, 5:5}" "for x in a: pass"
    1000000 loops, best of 3: 1.63 usec per loop
    % python2.4 -m timeit -c "a = {1:1, 2:2, 3:3, 4:4, 5:5}" "for x in a.keys(): pass"
    100000 loops, best of 3: 2.1 usec per loop


    Martin

    - --
    Homepage: http://www.cs.auc.dk/~factotum/
    GPG public key: http://www.cs.auc.dk/~factotum/gpgkey.txt
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.5 (GNU/Linux)
    Comment: Using Mailcrypt+GnuPG <http://www.gnupg.org>

    iEYEARECAAYFAkIUyPwACgkQYu1fMmOQldWV8ACdEZOgwwtOyp7c+hDVtQe+0LX1
    yRoAoITJGZA6zA0iE9G4lp2cP/GinlZC
    =njlP
    -----END PGP SIGNATURE-----
     
    Martin Christensen, Feb 17, 2005
    #2
    1. Advertising

  3. On 2005-02-17, <> wrote:
    > 1) Is there any advantage to use the
    >
    > y = a.keys()
    > for z in y:
    >
    > looping technique rather than the
    >
    > for x in a:
    >
    > looping technique?


    Not really.

    > 2) What are the tradeoffs for using each of the techniques?


    "for x in a" can be more efficient since it allows the
    dictionary to return key values one at a time instead of
    creating a list containing all of them. For small dictionaries
    it won't matter.

    Here's another choice, that's sometimes handy:

    >>> d = {1:'one',2:'two',3:'three'}
    >>> for k,v in d.items():

    ..... print k,v
    .....
    1 one
    2 two
    3 three
    >>>


    I wouldn't recommend this for large dictionaries.

    --
    Grant Edwards grante Yow! RELATIVES!!
    at
    visi.com
     
    Grant Edwards, Feb 17, 2005
    #3
  4. wrote:
    > 1) Is there any advantage to use the
    >
    > y = a.keys()
    > for z in y:
    >
    > looping technique rather than the
    >
    > for x in a:
    >
    > looping technique?
    >
    > 2) What are the tradeoffs for using each of the techniques?


    Calling dict.keys creates a list in memory of the keys to the dict.
    Using the dict directly in the for-loop (which implicitly calls
    __iter__) will only load one key into memory at a time. The only time
    you should call keys is if you *really* need a list. If you're just
    going to iterate over them in a for-loop, you should definitely use the
    latter technique.

    STeVe
     
    Steven Bethard, Feb 17, 2005
    #4
  5. Grant Edwards wrote:
    > Here's another choice, that's sometimes handy:
    >
    >>>>d = {1:'one',2:'two',3:'three'}
    >>>>for k,v in d.items():

    >
    > .... print k,v
    > ....
    > 1 one
    > 2 two
    > 3 three
    >
    > I wouldn't recommend this for large dictionaries.


    Yes, for large dictionaries, you should use:

    for k, v in d.iteritems():
    print k, v

    STeVe
     
    Steven Bethard, Feb 17, 2005
    #5
    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. Richard Coutts

    Newbie: iterating through folder contents

    Richard Coutts, Aug 30, 2003, in forum: C Programming
    Replies:
    1
    Views:
    334
    Richard Heathfield
    Aug 30, 2003
  2. JB
    Replies:
    2
    Views:
    405
  3. carl
    Replies:
    5
    Views:
    2,510
    James Kanze
    Nov 25, 2009
  4. Lise
    Replies:
    0
    Views:
    118
  5. Mafj
    Replies:
    4
    Views:
    270
Loading...

Share This Page