Home > Software design >  How to escape and loop with python inside curly brackets
How to escape and loop with python inside curly brackets

Time:12-16

For example, I have a list items=["a", "b", "c"]. And I want to escape and loop those in curly brackets for the requests body.

{
  ...
  "files": {
    for index,item in enumerate(items):
      "index1": "item1",
      "idex2": "item2",

  },
  ...
}

My wanted results is:

{
  ...
  "files": {
      "0": "a",
      "1": "b",
  },
  ...
}

CodePudding user response:

Not much is clear from you question, but is this what you want:

items = ["a", "b"]
results = {"files": {}}
for index, item in enumerate(items):
    results['files'][str(index)] = item
print (results)

Output:

{
    'files': {
        '0': 'a',
        '1': 'b'
    }
}
  • Related