vendredi 31 juillet 2020

Python: Referring to different dictionary items in a dictionary item

I have a dictionary:

region = {
"DACH": ["Germany", "Switzerland", "Austria"],
"BLX":["Netherlands", "Belgium", "Luxembourg"]
}

I would like to add an item like this:

region = {
"DACH": ["Germany", "Switzerland", "Austria"],
"BLX": ["Netherlands", "Belgium", "Luxembourg"],
"EUROPE": region["DACH"] + region["BLX"]
}

Python - rightfully so - complains about Unresolved reference: region.

I can solve this by using a lambda expression, like this:

region = {
"DACH": ["Germany", "Switzerland", "Austria"],
"BLX": ["Netherlands", "Belgium", "Luxembourg"],
"EUROPE": lambda: region["DACH"] + region["BLX"]
}

Are there more elegant ways to do this in Python? It doesn't feel pythonic to me, but rather glued together.

Aucun commentaire:

Enregistrer un commentaire