Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Solution to the previous quiz: [{'checkmate': True}, {'checkmate': True}]. Line 1 creates a list a with an empty dictionary. However, by coding * 2, we have duplicated the contents of the list with two dictionaries, but in a way that will be relevant later.
In line 2 we do the same thing. We see that the commutative property applies without problems, but here the list with elements to replicate is empty, so that actually b consists of an empty list.
Line 3 is more complex: a[len(b) - 1].update({'checkmate': True}). We see that what we do is to update the dictionary contained in the position len(b) - 1, which is -1, since we already saw that b is an empty list. Therefore, when accessing a[-1], what we do is to access the last position of the list a, which contains a dictionary.
Therefore, we update its content with the hardcoded dictionary {'checkmate': True}. However, when executing the last line of the script, we see that both dictionaries in list a have the same content and that is because when we did the duplication in line 1, we made copies by reference; something that is expected with mutable and complex objects such as dictionaries. In other words, both elements of the list, which are dictionaries, are the same. They both have references to the same dictionary.
This could be overlooked by an unsuspecting programmer using that notation in line 1. In short, you should know that it does not create a new dictionary, but creates as many references to the same dictionary as the number of list items you want.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Bienvenido a mi nueva serie de Quizzes de Programaci贸n, en la cual podr谩s poner a prueba tus conocimientos y habilidades sobre programaci贸n y desarrollo de software de una manera sencilla y divertida. Si quieres aprender m谩s sobre ella visita mi blog aqu铆 en Hive y el primer post donde la present茅.
Soluci贸n al quiz anterior: [{'checkmate': True}, {'checkmate': True}]. La l铆nea 1 crea una lista a con un diccionario vac铆o. Sin embargo, al codificar * 2, hemos duplicado el contenido de la lista con dos diccionarios, pero de una manera que ser谩 relevante m谩s adelante.
En la l铆nea 2 hacemos lo mismo. Vemos que la propiedad conmutativa se aplica sin problemas, pero aqu铆 la lista con elementos a replicar est谩 vac铆a, por lo que en realidad b consta de una lista vac铆a.
La l铆nea 3 es m谩s compleja: a[len(b) - 1].update({'checkmate': True}). Vemos que lo que hacemos es actualizar el diccionario contenido en la posici贸n len(b) - 1, que es -1, ya que ya vimos que b es una lista vac铆a. Por tanto, al acceder a a[-1], lo que hacemos es acceder a la 煤ltima posici贸n de la lista a, que contiene un diccionario.
Por lo tanto, actualizamos su contenido con el diccionario codificado {'checkmate': True}. Sin embargo, al ejecutar la 煤ltima l铆nea del script, vemos que ambos diccionarios en la lista a tienen el mismo contenido y eso se debe a que cuando hicimos la duplicaci贸n en la l铆nea 1, hicimos copias por referencia; algo que se espera con objetos mutables y complejos como los diccionarios. Es decir, ambos elementos de la lista, que son diccionarios, son iguales. Ambos tienen referencias al mismo diccionario.
Esto podr铆a ser pasado por alto por un programador desprevenido que use esa notaci贸n en la l铆nea 1. En resumen, debes saber que no se crea un nuevo diccionario, sino que crea tantas referencias al mismo diccionario como el n煤mero de elementos de la lista que desees.
Si quieres bloguear sobre contenido inform谩tico y de programaci贸n, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.