0

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!

5
  • Why do you want a list of dictionaries? Wouldn't a single large dictionary make more sense? Commented Feb 16, 2022 at 10:35
  • I try to get a single dictionary or JSON exactly like in the example I did. Commented Feb 16, 2022 at 10:38
  • The first example is a list of dictionaries. The second example is two dictionaries side by side. Neither example shows a single dictionary. Commented Feb 16, 2022 at 10:39
  • I don't know how to explain, I would like to combine Test School 2 -> 0 and Test School 2 -> 1 in one dictionary: { School Test1: { 0: { ... }, 1 { ... } } } Commented Feb 16, 2022 at 10:44
  • your expected outcome is a set of dicts, which is not valid in python Commented Feb 16, 2022 at 10:44

1 Answer 1

1

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()]
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome - just FYI: the reason you were getting confusing comments was that you put {} around two dictionaries, but didn't provide any keys. If you remove the {} around the two inner dicts, it becomes a single dict - I didn't catch the outer {} at first (if they were even there), but @enke correctly remarked that what you wrote was effectively a set of two dicts (which can't work, since a dict is not 'hashable'). The confusion arises from {1, 2} being a set and {1: 2} being a dict, both using {} in their literal form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.