By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,282 Members | 1,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,282 IT Pros & Developers. It's quick & easy.

accessing keys in dict

P: n/a
hi,

a_dict = {'name':'apple', 'color':'red', 'texture':'smooth',
'shape':'sphere'}

is there any difference between ..

for key in a_dict:

from

for key in a_dict.keys():
which is more preferred? any difference in performance?

THanks
james

Aug 15 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
In message <11**********************@l22g2000prc.googlegroups .com>,
james_027 wrote:
is there any difference between ..

for key in a_dict:

from

for key in a_dict.keys():
I'm assuming the former is equivalent to

for key in a_dict.iterkeys() :
Aug 25 '07 #2

P: n/a
On Aug 25, 7:41 am, Lawrence D'Oliveiro <l...@geek-
central.gen.new_zealandwrote:
In message <1187143106.021325.104...@l22g2000prc.googlegroups .com>,

james_027 wrote:
is there any difference between ..
for key in a_dict:
from
for key in a_dict.keys():

I'm assuming the former is equivalent to

for key in a_dict.iterkeys() :
Never assume. A better approach would be to experiment:

>>a_dict = {'name':'apple', 'color':'red', 'texture':'smooth',
'shape':'sphere'}
>>for i in a_dict: print i
color
shape
name
texture
>>for i in a_dict.iterkeys(): print i
color
shape
name
texture
>>for i in a_dict.itervalues(): print i
red
sphere
apple
smooth
>>for i in a_dict.iteritems(): print i
('color', 'red')
('shape', 'sphere')
('name', 'apple')
('texture', 'smooth')

Aug 25 '07 #3

P: n/a
On Sat, 25 Aug 2007 19:05:07 +0000, Dustan wrote:
Never assume. A better approach would be to experiment:
I assume that you would not like it if I poked you in the eye with a
sharp stick, but perhaps I better experiment...

*wink*

More seriously, if you wish to compare dict.keys() and dict.iterkeys(),
iterating over them doesn't help you because it shows only the
similarities, not the differences. But this will:
>>d = {1:"one", 2:"two"}
d.keys()
[1, 2]
>>d.iterkeys()
<dictionary-keyiterator object at 0xb7ecd280>

dict.keys() retrieves all the keys at once, up front, whether you need
them all or not. dict.iterkeys() returns an iterator which retrieves the
keys lazily, only when needed. Iterating over the dict itself is the same
as using iterkeys:
>>iter(d)
<dictionary-keyiterator object at 0xb7ecd4a0>
Performance-wise, the difference for small dicts may be trivial:
>>d = dict(zip(xrange(1000), xrange(1000)))
import timeit
timeit.Timer("for key in d: pass", "from __main__ import d").repeat()
[71.805022954940796, 71.10006308555603, 70.985043048858643]
>>timeit.Timer("for key in d.keys(): pass",
.... "from __main__ import d").repeat()
[80.323757886886597, 76.463414907455444, 76.681307792663574]

A six second difference on a million iterations of the loop is probably
not worth losing sleep over. But if your dict is very big, things may be
different:
>>d = dict(zip(xrange(1000000), xrange(1000000)))
timeit.Timer("for key in d: pass",
.... "from __main__ import d").repeat(number=100) # I don't have all day...
[9.2374939918518066, 8.3636147975921631, 8.3667001724243164]
>>timeit.Timer("for key in d.keys(): pass",
.... "from __main__ import d").repeat(number=100)
[13.776750087738037, 12.756224155426025, 12.697851181030273]

A four second difference on a hundred iterations of the loop _is_
something worth worrying about. Fortunately, Python makes it no worry at
all: just iterate on the dict regardless of whether your dict is small or
large, and you won't go wrong.
--
Steven.
Aug 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.