[Tutor] adding dictionary value at position [-1]

Norman Khine norman at khine.net
Sat Aug 6 15:18:47 CEST 2011


hello, thanks for the replies

On Sat, Aug 6, 2011 at 2:07 PM, Dave Angel <d at davea.name> wrote:
> On 08/06/2011 07:32 AM, Norman Khine wrote:
>>
>> hello,
>> i know that there are no indexes/positions in a python dictionary,
>> what will be the most appropriate way to do this:
>>
>>                 addresses = {}
>>                 for result in results.get_documents():
>>                     addresses[result.name] = result.title
>>                 # we add a create new address option, this needs to be
>> the last value
>>                 addresses['create-new-address'] = 'Create new address!'
>>
>>                # {"address-one": "Address One", "create-new-address":
>> "Create new address!", "address-two": "Address Two"}
>>
>>                 return dumps(addresses)
>>
>>
>> so that when i return the 'dumps(addresses)', i would like the
>> 'create-new-address' to be always at the last position.
>>
>> any advise much appreciated.
>>
>> norman
>>
> We can assume this is a fragment of a function, since it ends with a return.
>
> You don't say what this function is supposed to return, and you don't supply
> the source for dumps().

the function returns:

{"address-one": "Address One", "create-new-address": "Create new
address!", "zzz-address-two": "ZZZ Address Two"}

>
> So, applying my crystal ball and figuring you want a list, just write dumps
> so it puts the create-new-address entry at the end.  You could ensure that
> by changing the name create-new-address to zzz-create-new-address, and
> simply doing a sort.  Or you could simply return
>   return dumps(addresses) + "Create new address"
>
> and not put it into the dictionary at all.
> --
>
> DaveA
>
>



-- 
˙ʇı ɹoɟ ƃuıʎɐd ǝɹ,noʎ ʍou puɐ ǝɔıoɥɔ ɐ ʞooʇ ı ʇɐɥʇ sı 'ʇlnɔıɟɟıp sı ʇɐɥʍ
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] )


More information about the Tutor mailing list