Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question - iterating through dictionary object

Reply
Thread Tools

newbie question - iterating through dictionary object

 
 
mirandacascade@yahoo.com
Guest
Posts: n/a
 
      02-17-2005
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?

 
Reply With Quote
 
 
 
 
Martin Christensen
Guest
Posts: n/a
 
      02-17-2005
-----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>

iEYEARECAAYFAkIUyPwACgkQYu1fMmOQldWV8ACdEZOgwwtOyp 7c+hDVtQe+0LX1
yRoAoITJGZA6zA0iE9G4lp2cP/GinlZC
=njlP
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      02-17-2005
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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      02-17-2005
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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      02-17-2005
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
 
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
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
Iterating Through Dictionary of Lists JB Python 2 09-14-2009 05:48 PM
iterating through perl object fields Mafj Perl Misc 4 11-14-2008 09:56 AM
FileSystem Object - Iterating through a directory and building the path Lise ASP General 0 11-17-2003 09:07 PM
Newbie: iterating through folder contents Richard Coutts C Programming 1 08-30-2003 04:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57