-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

Preety Joshi
  • 31
  • 1
  • 6

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
  • 1,197
  • 1
  • 9
  • 17
-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

Preety Joshi
  • 31
  • 1
  • 6