Question:
I am using Python and I want to join these two dictionaries:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
How can I do it?
For now I had tried this, putting it all together on dic3
, but it seems to me that I do a lot of transformations and it is dic3
:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
#Uso el método items y list para crearme una lista
tuple_dic1 = list(dic1.items())
tuple_dic2 = list(dic2.items())
#los uno como lista y devuelvo un diccionario unido
dic3 = dict(tuple_dic1 + tuple_dic2)
On the other hand, dictionaries cannot have repeated keys, so in the case that there are repeated keys, what is the key that Python will keep? and How does it work?
For example, in these two cases that key 1
is repeated but with a different value:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?", 1:"Buenos días"}
Answer:
Python => 3.9
If the Python version is 3.9 or later, the operator |
and the |=
operator defined as merge and update operators respectively. So to join the dictionaries we would execute the following code:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
#Creamos un nuevo diccionario unión de los otros dos
dic3 = dic1 | dic2
print(dic3)
Exit:
{1: 'hola',
2: 'adios',
3: 'hasta luego',
4: '¿Cómo estás?',
5: '¿Te encuentras bien?'}
If we want to update an existing dictionary , we use the aforementioned operator |=
:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
#Actualizamos el diccionario dic1
dic1 |= dic2
print(dic1)
Exit:
{1: 'hola',
2: 'adios',
3: 'hasta luego',
4: '¿Cómo estás?',
5: '¿Te encuentras bien?'}
Although the output is the same, in the first output we have had to create a third dictionary and in the second, we have updated dic1
.
This can be found in the documentation for new versions of Python in a summarized form What's New In Python 3.9 . And in more detail in the PEP584 documentation
Versions prior to Python 3.9
This method is a great advance in the ease of use of Python, and replaces previous methods uglier or not so easily understood by people with little experience in the language.
Union of dictionaries before Python 3.9
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
#Aunque también era sencillo antes, la estrella no es conocida por todo el mundo,
#y no es lo primero que se le viene uno a la cabeza
{**dic1, **dic2}
Dictionary update before Python 3.9
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?"}
dic1.update(dic2)
What happens if we use the same password?
In both ways, the last key of the dictionary will always remain. It is logical that Python works like this, since the second dictionary would suppose an update on the first, therefore in case of repeated key the last dictionary always remains. Example:
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?", 1:"repito la clave"}
dic1 = {1:"hola", 2:"adios", 3:"hasta luego"}
dic2 = {4:"¿Cómo estás?", 5:"¿Te encuentras bien?", 1:"repito la clave"}
#Como tenemos la clave 1 repetida, el segúndo diccionario
#significa que está actualizando su valor
print(dic1 | dic2)
dic1 |= dic2
print(dic1)
Exit:
{1: 'repito la clave', 2: 'adios', 3: 'hasta luego', 4: '¿Cómo estás?', 5: '¿Te encuentras bien?'}
I repeat, this will happen the same in Python 3.9, as in previous versions