-1

In my code i am getting values like this

keys = request.POST.get('keys')
KeysList = json.loads(keys)
        
values = request.POST.get('values')
valuesList = json.loads(values)  

After print statement i am getting values in list like :

keys = ['A', 'B', 'C', 'D']

Values = ['true', 'false', 'true', 'true']

but what i exactly want is like i want a dictionary in this way :

updateObj = { 'A' : 'true', 'B' : 'false', 'C' : 'true', 'D' : 'true', }

how can i achieve this can any one please suggest me for this ?? i am stuck here thanks in advance

2 Answers2

2

You can use dict comprehension for this:

updatedObj = {key: value for key, value in zip(keys, Values)}

OR:

updatedObj = dict(zip(keys, Values)) # even more simpler than dict comprehesion

Hope this will solve your problem.

Mubashar javed
  • 789
  • 1
  • 5
  • 15
-1

keys = request.POST.get('keys') KeysList = json.loads(keys)

    values = request.POST.get('values')
    valuesList = json.loads(values)    
    res = {} 
    for key in KeysList: 
        for value in valuesList: 
            res[key] = value 
            valuesList.remove(value) 
            break 

I found the solution