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

Dave Angel d at davea.name
Sat Aug 6 14:07:08 CEST 2011


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().

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



More information about the Tutor mailing list