ash
ash

Reputation: 33

Create a dictionary of the list

I want to create a dictionary whose values are lists. For example:

"data": {"1": {"id": 1,
               "name": test1,
               "description":yyyyy},
         "2": {"id": 2,
               "name": test2,
               "description":xxxxx}}

When I do this, it just creates in the list - [] but I want to get all the values in the list as dictionary - {}:

data = []
for x in Test.objects.filter(act=True):
    data.append({"%s" % x.id:{"id":"%s" % x.id, "name": "%s" % x.name, "description": "%s" % x.description})
ins = {}
ins['instance'] = data

Result:

"data": [{"1": {"id": 1,
                "name": test1,
                "description":yyyyy}},
         {"2": {"id": 2,
                "name": test2,
                "description":xxxxx}}]

Upvotes: 1

Views: 118

Answers (2)

sytech
sytech

Reputation: 40871

First, you don't show any lists in your question. What you have there in your desired example is a dictionary whose values are also a dictionary. Brackets [] denote lists. Braces {} denote dictionaries. Lists are an ordered array of items. Dictionaries store values in key:value pairs.

Second, there's really no point to making a dictionary with keys numbered serially like that.You could simply have a list of dictionaries and refer to each item by the list index instead.

You should also name your variables for what they are rather than just x or test1 or yyyyy.

So as for your question, it sounds like you have some values like [1,test1,yyyyy] or say [x.id,x.name,x.description] where each item corresponds in order to: id,name, and description I believe the way you want to sort this data is as such:

data=[
    {"id":1,"name":test1,"description":yyyyy},
    {"id":2,"name":test2,"description":xxxxx}
]

So your code might look something like

data=[]
for x in Test.objects.filter(act=True):
    data.append({"id":x.id,"name":x.name,"description":x.description})

#Or as a list comprehension:
#data=[{"id":x.id,"name":x.name,"description":x.description} for x in Test.objects.filter(act=True)]

Then you can access your data like such

for item in data:
    print("""\
ITEM ID: {}
ITEM NAME: {}
ITEM DESCRIPTION: {}
---------------------""".format(item["id"],item["name"],item["description"]))

If you REALLY wanted to format your data exactly how you described in your post:

the_dict={}
for index, x in enumerate(Test.objects.filter(act=True)): #assuming this is a list of objects
    the_dict[str(index+1)]={"id":x.id,"name":x.name,"description":x.description}
    #the +1 to start keys at 1 as in your example, instead of 0

Edit: Actually, after re-reading your example, it sounds like you want the ID to be the key in the dictionary, not just numbered arbitrarily from 1-n...

In that case, as long as you're sure there's no duplicate IDs (dict keys must be unique) :

the_dict={}
for x in Test.objects.filter(act=True):
    the_dict[str(x.id)]={"id":x.id,"name":x.name,"description":x.description}

Upvotes: 1

TessellatingHeckler
TessellatingHeckler

Reputation: 28983

In your example, there are no lists. There is a dictionary with nested dictionaries. Try...

data = {}
for x in Test.objects.filter(act=True):
    data["%s" % x.id] = {"id":"%s" % x.id, "name": "%s" % x.name, "description": "%s" % x.description}

ins = {}
ins['instance'] = data

or

data = {}
for x in Test.objects.filter(act=True):
    data[str(x.id)] = {"id":str(x.id), "name": str(x.name), "description": str(x.description)}

ins = {}
ins['instance'] = data

Upvotes: 1

Related Questions