473,550 Members | 2,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preferred Python idiom for handling non-existing dictionary keys and why?


Hello again All!

(First, I would like to mention I did try to google
for the answer here!)

Say I am populating a dictionary with a list and
appending. I have written it thusly:

d={}
for (k,v) in somedata():
try:
d[k].append(v)
except KeyError:
d[k]=[v]

I could have written:

d={}
for (k,v) in somedata():
if (k in d):
d[k].append(v)
else:
d[k]=[v]
Which is perferred and why? Which is "faster"?

Thanks!!

Quentin
=====
-- Quentin Crain

------------------------------------------------
I care desperately about what I do.
Do I know what product I'm selling? No.
Do I know what I'm doing today? No.
But I'm here and I'm gonna give it my best shot.
-- Hansel

_______________ _______________ ____
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

Jul 18 '05 #1
8 2736
Quentin Crain <cz***@yahoo.co m> writes:
(First, I would like to mention I did try to google
for the answer here!)

Say I am populating a dictionary with a list and
appending. I have written it thusly:

[...]

You want the dict.setdefault method.
John
Jul 18 '05 #2

"Quentin Crain" <cz***@yahoo.co m> wrote in message
news:ma******** *************** *********@pytho n.org...

Hello again All!

(First, I would like to mention I did try to google
for the answer here!)
The question you asked is frequent, but hard to isolate, and has
special-case answer (to question you did not ask) even harder to find.
Say I am populating a dictionary with a list and
appending. I have written it thusly:

d={}
for (k,v) in somedata():
try:
d[k].append(v)
except KeyError:
d[k]=[v]

I could have written:

d={}
for (k,v) in somedata():
if (k in d):
d[k].append(v)
else:
d[k]=[v]
Which is perferred and why?
Neither. For me, both are superceded by

d={}
for (k,v) in somedata():
d[k] = d.get(k, []).append(v)

Read Library Reference 2.2.7 Mapping Types to learn current dict
methods.
Which is "faster"?


Tradeoff is small extra overhead every loop (the condition) versus
'occasional' big overhead (exception catching). Choice depends on
frequency of exceptions. As I remember, one data-based rule of thumb
from years ago is to use conditional if frequency more that 10%. You
could try new timeit() on all three versions.

Terry J. Reedy
Jul 18 '05 #3
Say I am populating a dictionary with a list and appending. I have
written it thusly:


John> You want the dict.setdefault method.

d.setdefault() never made any sense to me (IOW, to use it I always had to
look it up). The semantics of what it does just never stick in my brain.
Consequently, even though it's less efficient I generally write such loops
like this:

d = {}
for (key, val) in some_items:
lst = d.get(key) or []
lst.append(val)
d[key] = lst

Note that the first statement of the loop is correct (though perhaps not
obvious at first glance), since once initialized, d[key] never tests as
False. FYI, timeit tells the performace tale:

% timeit.py -s 'd={}' 'x = d.setdefault("x ", [])'
1000000 loops, best of 3: 1.82 usec per loop
% timeit.py -s 'd={}' 'x = d.get("x") or [] ; d["x"] = x'
100000 loops, best of 3: 2.34 usec per loop

But my way isn't bad enough for me to change. ;-)

Skip

Jul 18 '05 #4
Skip Montanaro wrote:
>> Say I am populating a dictionary with a list and appending. I have
>> written it thusly:


John> You want the dict.setdefault method.

d.setdefault() never made any sense to me (IOW, to use it I always had to
look it up).


I have had a wrong idea about setdefault for a very long time. To me, it
sounds like: "set this value the default value of dict, so after this
call, let each non-existing key result in this value".

Gerrit.

--
261. If any one hire a herdsman for cattle or sheep, he shall pay him
eight gur of corn per annum.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #5
In article <jO************ ********@comcas t.com>, Terry Reedy wrote:

"Quentin Crain" <cz***@yahoo.co m> wrote in message
news:ma******** *************** *********@pytho n.org...

Which is perferred and why?


Neither. For me, both are superceded by

d={}
for (k,v) in somedata():
d[k] = d.get(k, []).append(v)


Not quite... append returns None, so you'll need to write that as two
separate statements, ie.:

d[k] = d.get(k, [])
d[k].append(v)

Or, just:

d.setdefault(k, []).append(v)

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Jul 18 '05 #6
Terry Reedy wrote:
Neither. For me, both are superceded by

d={}
for (k,v) in somedata():
d[k] = d.get(k, []).append(v)


This would have to look up the key twice, so it has no advantage over

if k in d:
d[k].append(v)
else:
d[k] = [v]

Anyway, list.append() returns always None, so it does not work.
I think you mean

d = {}
for (k, v) in somedata:
d.setdefault(k, []).append(v)

There is a small overhead for throwing away a new list object if the key is
already in the dictionary, but I don't really care.
(Or is the compiler smart enough to reuse the empty list?)

Peter
Jul 18 '05 #7
Skip Montanaro wrote:
...
% timeit.py -s 'd={}' 'x = d.setdefault("x ", [])'
1000000 loops, best of 3: 1.82 usec per loop
% timeit.py -s 'd={}' 'x = d.get("x") or [] ; d["x"] = x'
100000 loops, best of 3: 2.34 usec per loop

But my way isn't bad enough for me to change. ;-)


Actually, you can still do a bit better w/o using setdefault:

[alex@lancelot pop]$ timeit.py -s'd={}' 'x=d.setdefault ("x",[])'
1000000 loops, best of 3: 0.925 usec per loop
[alex@lancelot pop]$ timeit.py -s'd={}' 'x=d.get("x") or []; d["x"]=x'
1000000 loops, best of 3: 1.21 usec per loop
[alex@lancelot pop]$ timeit.py -s'd={}' 'x=d.get("x",[]); d["x"]=x'
1000000 loops, best of 3: 1.13 usec per loop

as d.get takes a second optional argument, you can still save the 'or'.
Alex

Jul 18 '05 #8

"Terry Reedy" <tj*****@udel.e du> wrote in message
news:jO******** ************@co mcast.com...
Read Library Reference 2.2.7 Mapping Types to learn current dict
methods.


Seeing the other responses, I see I need to do the same and read about
setdefault() ;-)
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
11650
by: Catalin | last post by:
Can Python replace PHP? Can I use a python program to make an interface to a mysql 4.X database? If that's possible where can I find a tutorial?
0
1313
by: Dave Benjamin | last post by:
I just noticed that the "new" module is deprecated in Python 2.3. Since the old way of adding a method to a particular instance (not its class) was to use new.instancemethod, I am guessing that we are now supposed to use types.MethodType. Is this the preferred idiom for adding an instance method? import new import types class...
49
2800
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville Vainio http://www.students.tut.fi/~vainio24
0
359
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 235 open ( -6) / 2633 closed (+11) / 2868 total ( +5) Bugs : 767 open ( +3) / 4463 closed (+10) / 5230 total (+13) RFE : 151 open ( +1) / 131 closed ( +0) / 282 total ( +1) New / Reopened Patches ______________________
68
5799
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
137
6982
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain...
112
13763
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and...
4
2622
by: tleeuwenburg | last post by:
To whom it may concern, I have been involved in putting together a new Python journal, called (oh so originally) The Python Journal. This isn't related to a previous project also called The Python Journal although we have the approval of the group that put it together in using their name. http://pyjournal.cgpublisher.com is the journal...
0
7488
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7520
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7845
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3532
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3517
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1094
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.