Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75452 > unrolled thread

Re: Dict when defining not returning multi value key error

Started byNed Batchelder <ned@nedbatchelder.com>
First post2014-07-31 21:35 -0400
Last post2014-07-31 22:32 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Dict when defining not returning multi value key error Ned Batchelder <ned@nedbatchelder.com> - 2014-07-31 21:35 -0400
    Re: Dict when defining not returning multi value key error Roy Smith <roy@panix.com> - 2014-07-31 22:32 -0400

#75452 — Re: Dict when defining not returning multi value key error

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-07-31 21:35 -0400
SubjectRe: Dict when defining not returning multi value key error
Message-ID<mailman.12498.1406856928.18130.python-list@python.org>
On 7/31/14 5:15 PM, Ian Kelly wrote:
> On Thu, Jul 31, 2014 at 5:24 AM, Dilu Sasidharan <dilu.seven@gmail.com> wrote:
>> Hi,
>>
>> I am wondering why the dictionary in python not returning multi value key
>> error when i define something like
>>
>> p = {'k':"value0",'k':"value1"}
>>
>> key is string immutable and sometimes shares same id.
>>
>> also if the key is immutable and have different ids.
>>
>> like
>>
>> p = {'1':"value0",'1.0':"value1"}
>
> In this latter case note that '1' and '1.0' are not equal, so this
> will simply result in two separate entries in the dict anyway.
>

You might want to check first:

     Python 2.7.5 (default, Aug  5 2013, 19:47:08)
     >>> 1 == 1.0
     True
     >>> {1.0: None, 1: None}
     {1.0: None}

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [next] | [standalone]


#75453

FromRoy Smith <roy@panix.com>
Date2014-07-31 22:32 -0400
Message-ID<roy-0B5DC0.22323631072014@news.panix.com>
In reply to#75452
In article <mailman.12498.1406856928.18130.python-list@python.org>,
 Ned Batchelder <ned@nedbatchelder.com> wrote:

> On 7/31/14 5:15 PM, Ian Kelly wrote:
> > On Thu, Jul 31, 2014 at 5:24 AM, Dilu Sasidharan <dilu.seven@gmail.com> 
> > wrote:
> >> Hi,
> >>
> >> I am wondering why the dictionary in python not returning multi value key
> >> error when i define something like
> >>
> >> p = {'k':"value0",'k':"value1"}
> >>
> >> key is string immutable and sometimes shares same id.
> >>
> >> also if the key is immutable and have different ids.
> >>
> >> like
> >>
> >> p = {'1':"value0",'1.0':"value1"}
> >
> > In this latter case note that '1' and '1.0' are not equal, so this
> > will simply result in two separate entries in the dict anyway.
> >
> 
> You might want to check first:
> 
>      Python 2.7.5 (default, Aug  5 2013, 19:47:08)
>      >>> 1 == 1.0
>      True
>      >>> {1.0: None, 1: None}
>      {1.0: None}

Yeah, but '1' != '1.0' in any version of Python.  Might be equal in PHP, 
though :-)

As for the original question, there's nothing that says you can't assign 
multiple times to the same key in a dictionary.  The keys may be 
immutable, but the values aren't.  If you assign to an existing key, it 
just overwrites the value.  I assume that when it sees something like:

p = {'k':"value0", 'k':"value1"}

it effectively treats that as if you had written:

p = {}
p['k'] = "value0"
p['k'] = "value1"

and just overwrites the first assignment with the second.  If you want 
something akin to C++'s multimap, you probably want defaultdict(set) or 
defaultdict(list).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web