-3

This is the data:

data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}] 

data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]

expected Output:

list1:[{"name":"vikash","roll":39,"hobby":"football","food":"any"},{"name":"kumar","roll":3,"hobby":"basketball","food":"any"}]

Please help me!

YUVI_1303
  • 112
  • 8
Vikash
  • 1

2 Answers2

0

Providing both lists are the same length, you're happy there are no key conflicts and you're using Python...

data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]

data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]


list1 = []

for k, v in enumerate(data):
    list1.append({**v, **data2[k]})

print(list1)

However in the future please be more concise when asking your questions.

  • Specify the language
  • Don't include redundant tags (eg - this question has nothing to do with Django)
  • Tell us what you've tried!

Also... the "please help me" line isn't required. The whole purpose of this community is to help each other so again this is redundant!

Mat
  • 1,345
  • 9
  • 15
0

A simpler to understand solution

data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}] 

data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]


for i in range(len(data)):
    del data[i]['id']
    d_main = {}
    d_main.update(data[i])
    d_main.update(data2[i])
    list1.append(d_main)
    
print(list1)
YUVI_1303
  • 112
  • 8