dict.items to accept optional iterable with keys to use
4 Apr
2012
4 Apr
'12
1:07 a.m.
Sometimes you want a dict which is subset of another dict. It would nice if dict.items accepted an optional list of keys to return. If no keys are given - use default behavior - get all items. class NewDict(dict): def items(self, keys=()): """Another version of dict.items() which accepts specific keys to use.""" for key in keys or self.keys(): yield key, self[key] a = NewDict({ 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}) print(dict(a.items()))print(dict(a.items((1, 3, 5)))) vic@ubuntu:~/Desktop$ python test.py {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}{1: 'one', 3: 'three', 5: 'five'} Thanks for the attention. -- *Victor Varvariuc*
4397
Age (days ago)
4397
Last active (days ago)
3 comments
4 participants
participants (4)
-
Chris Rebert
-
Steven D'Aprano
-
Sven Marnach
-
Victor Varvariuc