-2

I'm trying to iterate and map list with a key pair. I'm getting the following output as below(i.e.) Im getting only the last value in the list.

{'first': [{'Key': 'Created', 'Value': '2020'}, {'Key': 'Type', 'Value': 'Entr'}], 'Second': [{'Key': 'Created', 'Value': '2020'}, {'Key': 'Type', 'Value': 'Entr'}]}

Code

tags = [[{'Key': 'Created', 'Value': '2019'}, {'Key': 'Type', 'Value': 'Business'}], [{'Key': 'Created', 'Value': '2020'}, {'Key': 'Type', 'Value': 'Entr'}]]

samplelist = ['first','Second']

sampledict = {}

for i in samplelist:
    for tag in tags:
        sampledict[i] = tag

Expected Output

{'first': [{'Key': 'Created', 'Value': '2019'}, {'Key': 'Type', 'Value': 'Business'}], 'Second': [{'Key': 'Created', 'Value': '2020'}, {'Key': 'Type', 'Value': 'Entr'}]}
chluebi
  • 1,809
  • 1
  • 8
  • 21
Beginner
  • 1
  • 3

2 Answers2

0

You might use dict and zip combination as follows:

dict(zip(samplelist, tags))

Note: You might not aware of that but you have one missing quote sign. tags should be like this:

tags = [[{'Key': 'Created', 'Value': '2019'}, {'Key': 'Type', 'Value': 'Business'}], [{'Key': 'Created', 'Value': '2020'}, {'Key': 'Type', 'Value': 'Entr'}]]
Baris
  • 397
  • 5
  • 12
-1
tags = [[{'Key': 'Created', 'Value': '2019'},
         {'Key': 'Type', 'Value': 'Business'}],
        [{'Key': 'Created', 'Value': '2020'},
         {'Key': 'Type', 'Value': 'Entr'}]]

samplelist = ['first', 'Second']
sampledict = {}
i=0
while i < len(samplelist):
    sampledict[samplelist[i]] = tags[i]
    i += 1
Houda
  • 671
  • 6
  • 16