Python | How to create dynamic and expandable dictionaries

Posted by MMRUser on Stack Overflow See other posts from Stack Overflow or by MMRUser
Published on 2010-03-18T08:26:30Z Indexed on 2010/03/18 8:31 UTC
Read the original article Hit count: 205

Filed under:

I want to create a Python dictionary which holds values in a multidimensional accept and it should be able to expand, this is the structure that the values should be stored :-

userdata = {'data':[{'username':'Ronny Leech','age':'22','country':'Siberia'},{'username':'Cronulla James','age':'34','country':'USA'}]}

Lets say I want to add another user

def user_list():
     users = []
     for i in xrange(5, 0, -1):
       lonlatuser.append(('username','%s %s' % firstn, lastn))
       lonlatuser.append(('age',age))
       lonlatuser.append(('country',country))
     return dict(user)

This will only returns a dictionary with a single value in it (since the key names are same values will overwritten).So how do I append a set of values to this dictionary.

Note: assume age, firstn, lastn and country are dynamically generated.

Thanks.

© Stack Overflow or respective owner

Related posts about python