Most Pythonic way to merge two dictionnaries having common key/value pair

2024/9/20 19:44:11

I have two lists of python dictionnaries :

l1 = [{"id":1, "name":"A"}, {"id":2, "name":"B"}]
l2 = [{"id":1, "full_name":"Alfred"}, {"id":2, "full_name":"Barbara"}]

My goal is to have a list as follow :

l3 = [{"id":1, "full_name":"Alfred", "name":"A"}, {"id":2, "full_name":"Barbara", "name":"B"}]

I think I could use nested loops to do this, but I'm sure it should be possible to do it in a better and more pythonic way.

Answer
from collections import defaultdictres = defaultdict(dict)
for a, b in zip(l1, l2):key1 = a['id']key2 = b['id']res[key1].update(a)res[key2].update(b)print(list(res.values())) # [{'id': 1, 'name': 'A', 'full_name': 'Alfred'}, {'id': 2, 'name': 'B', 'full_name': 'Barbara'}]

Although this would work in a single iteration you might want to simplify your underlying data structures a bit to make them easier to work with.

https://en.xdnf.cn/q/119291.html

Related Q&A

How to close a while True loop instantly Python

I have a problem ... How can i press P on my keyboard and close the entire program faster ( i would like instantly ) ? The script that i made runs in a loop ( Loop B ) and checks for an image on deskt…

How to replace a value in a list

the program asks user to enter 5 unique number, if the number is already in the list, ask for a new number. after 5 unique numbers have been entered, display the listnumbers = [1,2,3,4,5] count = 0 ind…

How To Reverse A Nested Python List

Given the following nested list,myList=([1,[2,3],[[4,5,[6],7],8,9]])I want to reverse it to be converted into:myList= [[[4, 5, [6], 7], 8, 9], [2, 3], 1]How do I do that? Thanks.

Extract street address from a string

Is there any way to extract a street address from a string (say, email) using python? The address does not come in a set format. It can come without state, zip code, city, but I can guess and supply t…

Convert list in String format back to list of float numbers

str = [ 3.82133931e-01 4.27354313e-02 1.94678816e-03 0.00000000e+000.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+000.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e…

Hawaiian Pronunciation [duplicate]

This question already has an answer here:Hawaiian pronouncer(1 answer)Closed 1 year ago.Hitting a snag with an assignment and thought Id ask for help. The goal is to be able to pronounce Hawaiian words…

mutiline python script in html (pyscript)

Ive tried to use pyscript in html but i can only get it to work in one line of code can somebody help me get it to work for the following code? def vpn(website):from selenium import webdriverfrom sele…

How to write an output of a command to stdout and a file in Python3?

I have a Windows command which I want to write to stdout and to a file. For now, I only have 0 string writen in my file:#!/usr/bin/env python3 #! -*- coding:utf-8 -*-import subprocesswith open(auto_cha…

Mongodb adding a new field in an existing document, with specific position

I am facing this issue where I need to insert a new field in an existing document at a specific position. Sample document: { "name": "user", "age" : "21", "…

how to check every 3 x 3 box in sudoku?

I am trying to build a sudoku solver without much googling around. Right now, I am working on a function to test whether the board is valid, which I will use later in a loop. This is what the function …