All Questions

143
votes
15answers
72450 views

Use cases for the 'setdefault' dict method

The addition of collections.defaultdict in Python 2.5 greatly reduced the need for dict's setdefault method. This question is for our collective education: What is setdefault still useful for, today...
6
votes
1answers
7315 views

Why does this python dictionary get created out of order using setdefault()?

I'm just starting to play around with Python (VBA background). Why does this dictionary get created out of order? Shouldn't it be a:1, b:2...etc.? class Card: def county(self): c = 0 l = 0...
31
votes
5answers
27235 views

python dict: get vs setdefault

The following two expressions seem equivalent to me. Which one is preferable? data = [('a', 1), ('b', 1), ('b', 2)] d1 = {} d2 = {} for key, val in data: # variant 1) d1[key] = d1.get(key, ...
16
votes
1answers
4360 views

Is the defaultdict in Python's collections module really faster than using setdefault?

I've seen other Python programmers use defaultdict from the collections module for the following use case: from collections import defaultdict s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue'...

Previous Next