Home > Net >  Combine two dictionaries into one in Python
Combine two dictionaries into one in Python

Time:02-16

after having carried out unsuccessful research and tests, I have decided to write here. I am trying to merge two dictionaries with the same key into one dictionary by merging the duplicate values. Specifically, I have this code: schools =

firestore.collection('schools').document(regione).collection(provincia).document(comune).collections()
message = []
for collection in schools:
    print(collection.id)
    for doc in collection.stream():
        
        message.append({
            collection.id: {
                doc.id: doc.to_dict()
            }
        })
print(message)
return message

I also tried to insert the return after the for, but it just takes the first value only.

Which as a result gives me:

[
    {
        "Test school": {
            "0": {
                "indirizzo": "Test",
                "INFORMATICA E TELECOMUNICAZIONI": {
                    "3A INF": "G1H8JF"
                }
            }
        }
    },
    {
        "Test School2": {
            "0": {
                "TEST": {
                    "4CCC": "2g45r"
                },
                "indirizzo": "GG1"
            }
        }
    },
    {
        "Test School2": {
            "1": {
                "indirizzo": "gg2",
                "gg": {
                    "asas": "3r3"
                }
            }
        }
    }
]

I want to get this result instead:

{
    {
        "Test School1": {
            "0": {
                "indirizzo": "Test",
                "INFORMATICA E TELECOMUNICAZIONI": {
                    "3A INF": "G1H8JF"
                }
            }
        }
    },
    {
        "Test School2": {
            "0": {
                "TEST": {
                    "4CCC": "2g45r"
                },
                "indirizzo": "GG1"
            },
            "1": {
                  "indirizzo": "gg2",
                  "gg": {
                      "asas": "3r3"
                  }
              }
        }
    }
}

I have tried various codes but I cannot adapt them in my specific case on how to merge the two dictionaries. I hope you can help me out. collection.id contains the name of the school ("Test School1")

doc.id contains the school account ("0")

doc.to_dict() contains the address and all other information

I use Python 3.9.X

I want to clarify that I have also read various answers on this site and I believe that for this specific case it is not a duplicate.

Thanks so much!

CodePudding user response:

The easiest way of keeping track of what you already have somethinf of, is in a dictionary:

from collections import defaultdict

firestore.collection('schools').document(regione).collection(provincia).document(comune).collections()
message = defaultdict(dict)
for collection in schools:
    print(collection.id)
    for doc in collection.stream():
        message[collection.id][doc.id] = doc.to_dict()
print(message)
return message

If you need everything to be split up in multiple dictionaries, you can then break it up afterwards:

final_result = [{school: d} for school, d in message.items()]
  • Related