newbie question - iterating through dictionary object

M

mirandacascade

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.
.... print x
.... print a[x]
....
1
a
2
b.... 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?
 
M

Martin Christensen

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
G

Grant Edwards

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:
..... print k,v
.....
1 one
2 two
3 three
I wouldn't recommend this for large dictionaries.
 
S

Steven Bethard

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
 
S

Steven Bethard

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


.... 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top