Home > Blockchain >  Replace values in a list of dictionaries with duplicate keys, it updates both indexes even if one is
Replace values in a list of dictionaries with duplicate keys, it updates both indexes even if one is

Time:11-25

I want to replaces the options label and value input text, I am able to access them and updated them but it updates all the values not just one label or the input text but all the indexes.

   response=[ {'arr': [{'title': 'Wählen Sie das passende Rechtsgebiet:', 
    'options': [{'label': 'Monday 1:00 pm', 'value': {'input': {'text': 'Monday 1:00 pm'}}}, 
    {'label': 'Monday 1:00 pm', 'value': {'input': {'text': 'Monday 1:00 pm'}}}], 
    'description': '', 'response_type': 'option'}]}]

My code to update the dictionary values it works but it updates both index with the same value:

response['arr'][0]['options'][0].update({'label': "Contract Law"})
response['arr'][0]['options'][0]['value']['input'].update({'text': "Contract Law"})
response['arr'][0]['options'][1].update({'label': "Sales law"})
response['arr'][0]['options'][1]['value']['input'].update({'text': ""Sales law""})

Result: enter image description here

Complete Code:

import numpy as np
list_foci=["Erbschaftssteuerrecht","Erbrecht"]
list_size=len(list_foci)
dynamic_list= [
    {
      "title": "What time do you want your appointment?",
      "options": [
        {
          "label": "Monday 1:00 pm",
          "value": {
            "input": {
              "text": "Monday 1:00 pm"
            }
          }
        }
      ],
      "description": "",
      "response_type": "option"
    }
  ]
#for creating new values from the first index options e.g. label,value etc....
updated=list(np.repeat(dynamic_list[0]['options'], list_size))
response={
  "arr": [
    {
      "title": "Wählen Sie das passende Rechtsgebiet:",
      "options": updated,
      "description": "",
      "response_type": "option"
    }
  ]
  }
response['arr'][0]['options'][0]['label']= "Contract Law"
response['arr'][0]['options'][0]['value']['input']['text'] = "Contract Law"
response['arr'][0]['options'][1]['label']= "Sales Law"
response['arr'][0]['options'][1]['value']['input']['text'] = "Sales Law"
print(response)

CodePudding user response:

What about something that doesn't use update()?

response[0]["arr"][0]["options"][1]["value"]["input"]["text"] = "Contract Law"

Result:

In [1]: response
Out[1]: 
[{'arr': [{'title': 'Wählen Sie das passende Rechtsgebiet:',
    'options': [{'label': 'Monday 1:00 pm',
      'value': {'input': {'text': 'Monday 1:00 pm'}}},
     {'label': 'Monday 1:00 pm',
      'value': {'input': {'text': 'Contract Law'}}}],
    'description': '',
    'response_type': 'option'}]}]

CodePudding user response:

from copy import deepcopy
list_foci=["Erbschaftssteuerrecht","Erbrecht"]
list_size=len(list_foci)
dynamic_list={
  "arr": [
    {
      "title": "What time do you want your appointment?",
      "options": "foci",
      "description": "",
      "response_type": "option"
    }
  ]
}
foci=[
    {
        "label": "Monday 1:00 pm",
        "value": {
            "input": {
                "text": "Monday 1:00 pm"
            }
        }
    }
]
copy_size=list_size-1
for x in range(copy_size):
    foci.append(deepcopy(foci[0]))
print(copy_size)
for foci_value in range(list_size):
    foci[foci_value]['label']=list_foci[foci_value]
    foci[foci_value]['value']['input']['text']=list_foci[foci_value]
print(foci)
  • Related