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

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Sat Aug 6 13:59:47 CEST 2011


Resending to list, since I click reply instead of reply to list, I'm 
sorry Norman since you are receiving twice:

If you need an order item, use a list, so you can use a dictionary 
containing an ordered list of dictionaries. I think you want do somthing 
like this:

import json
addresses = {}
addresses['values'] = []
values = [1, 2, 3]

for index,value in enumerate(values):
     addresses['values'].append({index: value})

print(json.dumps(addresses))

Output will be:

{"values": [{"0": 1}, {"1": 2}, {"2": 3}]}

On 06/08/11 13:32, 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
>



More information about the Tutor mailing list