Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
How to update dictionaries inside two separate lists?

I have two list :

list1 = [{"name":"xyz" ,"roll":"r" , "sap_id":"z"} , {"name":"pqr" ,"roll":"s" , "sap_id":"w"}]
list2 = [{"cn_number":"26455"} , {"cn_number":"26456"}]

I want a new list like:

new_list = [{"name":xyz ,"roll":"r" , "sap_id":"z","cn_number":"26455"} , {"name":"pqr" ,"roll":"s" , "sap_id":"w","cn_number":"26456"}]

I tried the following method:

new_list = [i.update(j) for i, j in zip(list1, list2)]

but got some nasty error

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This syntax works for python3.5+ to merge two dictionaries z = {**x, **y}

Python 3.7.0 (default, Jun 28 2018, 13:15:42)
>>> list1 = [{"name":"xyz" ,"roll":"r" , "sap_id":"z"} , {"name":"pqr" ,"roll":"s" , "sap_id":"w"}]
>>> list2 = [{"cn_number":"26455"} , {"cn_number":"26456"}]
>>> new_list = [{**i, **j} for i, j in zip(list1, list2)]
>>> new_list
[{'name': 'xyz', 'roll': 'r', 'sap_id': 'z', 'cn_number': '26455'}, {'name': 'pqr', 'roll': 's', 'sap_id': 'w', 'cn_number': '26456'}]

More information: How do I merge two dictionaries in a single expression (take union of dictionaries)?

over 4 years ago · Santiago Trujillo Relatório

0

You can use the Python 3.5+ dict merging syntax:

new_list = [{**a, **b} for (a, b) in zip(list1, list2)]

results in

[
  {'name': 'xyz', 'roll': 'r', 'sap_id': 'z', 'cn_number': '26455'}, 
  {'name': 'pqr', 'roll': 's', 'sap_id': 'w', 'cn_number': '26456'},
]
over 4 years ago · Santiago Trujillo Relatório

0

An efficient way to do this is with collections.ChainMap which is made specifically for combining multiple dictionaries. Read more on the documentation here.

from collections import ChainMap

[dict(ChainMap(i,j)) for i,j in zip(list1, list2)]
[{'cn_number': '26455', 'name': 'xyz', 'roll': 'r', 'sap_id': 'z'},
 {'cn_number': '26456', 'name': 'pqr', 'roll': 's', 'sap_id': 'w'}]

NOTE: Depending on how you want to use each element of the updated list, you could use a generator and avoid using dict() over ChainMap object to still get what you need without using unnecessary memory! ChainMap gives you a single updatable view of the multiple dictionaries by still referencing the original objects.

#efficient way -

#EDITED: lesser keystrokes as suggested by @Kelly
g = map(ChainMap, list1, list2) 

#g = (ChainMap(i,j) for i,j in zip(list1, list2))

next(g).get('sap_id')
z
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda