Home > Software design >  Duplicate dictionary within a list
Duplicate dictionary within a list

Time:11-25

I want to duplicate the dictionary within the list and append it back to the list.

foci=[
        {
          "label": "Monday 1:00 pm",
          "value": {
            "input": {
              "text": "Monday 1:00 pm"
            }
          }
        }
      ]

CodePudding user response:

You'll need to deepcopy the nested dict and append it to the list:

from copy import deepcopy

foci = [
    {
        "label": "Monday 1:00 pm",
        "value": {
            "input": {
                "text": "Monday 1:00 pm"
            }
        }
    }
]

foci.append(deepcopy(foci[0]))

print(foci)
  • Related