[Python-ideas] dictionary constructor should not allow duplicate keys

Steven D'Aprano steve at pearwood.info
Wed May 4 12:24:13 EDT 2016


On Wed, May 04, 2016 at 09:09:22AM -0700, Ethan Furman wrote:
> On 05/04/2016 07:20 AM, Steven D'Aprano wrote:

> >and it matches the behaviour of both the dict constructor and dict
> >comprehensions.
> 
> Okay, we had dict displays and dicts, and now you say dict 
> constructor... do you mean dict()?  Because:

I was thinking of dict with a list of tuples as argument:

py> dict([(1, 'a'), (2, 'b'), (1, 'c')])
{1: 'c', 2: 'b'}

or an object with a keys method:

py> class K:
...     def keys(self):
...             return [1, 2, 1]
...     def __getitem__(self, i):
...             return 'abcd'[i]
...
py> dict(K())
{1: 'b', 2: 'c'}




-- 
Steve


More information about the Python-ideas mailing list