0

Not able to get proper output when tried to use 2 list to make one dictionary

Input is a name list and image list.

name_list = ["Brad Pitt", "Chris Evans"]
image_list = ["http://image_1.png","http://image_2.png"]

-

dict = []   
for item in name_lst:
    new_item = []
    new_item.extend(item.split())
    for img in img_url_lst:
        new_img.append(img)
        dict.append({"FirstName:" : new_item[0], "Lastname:" : new_item[1], "ImageURL:":new_img[img]})      
return dict

-

"actors": [
 {
  "firstName":"Johnny",
  "lastName":"Depp",
  "imageURL":"http://imageurl/1"
 },
 {
  "firstName":"Ricky",
  "lastName":"Martin",
  "imageURL":"http://imageurl/2"
 }
 ],

Can somebody help me merge two lists and make a dictionary out of it.

Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • The computer will do exactly what you told it to do, and you'll get as many Brad Pitts and Chris Evanses (?) as there are images in `image_list`. Is that what you intended to write? Or did you want to have a one-to-one correspondence between the actors' names and the images? – ForceBru Jul 12 '19 at 08:46
  • duplicate https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary – Ashu Jul 12 '19 at 11:24

1 Answers1

1
name_list = ["Brad Pitt", "Chris Evans"]
image_list = ["http://image_1.png","http://image_2.png"]

dict = {}
actors = []
for i in range(len(name_list)):
    dict["firstName"] = name_list[i].split()[0]
    dict["lastName"] = name_list[i].split()[1]
    dict["imageURL"] = image_list[i]
    # or
    # dict.update({"firstName": name_list[i].split()[0], "lastName": name_list[i].split()[1], "imageURL": image_list[i]})
    actors.append(dict)

print (actors)

output:

[{'firstName': 'Chris', 'lastName': 'Evans', 'imageURL': 'http://image_2.png'}, {'firstName': 'Chris', 'lastName': 'Evans', 'imageURL': 'http://image_2.png'}]
ncica
  • 7,015
  • 1
  • 15
  • 37