0

En el ejemplo de abajo, no entiendo porqué obtengo False como salida de print(pairs.get(1))

pairs = {"1": "manzana", 2: "dos", 1: "apple", 3: "pera", "orange": [2, 3, 4], True: False, None: "True", } print(pairs.get(1)) print(pairs.get("1")) print(pairs.get(1)) print(pairs.get(2)) print(pairs.get(3)) print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(12345, "not in dictionary"))

1st Apr 2019, 8:02 PM
Rocio Grande
Rocio Grande - avatar
2 Respostas
+ 1
This is the problem: 1: "apple", True: False, True is evaluated to 1 and as it already exists as a dictionary key, the first value associated to 1, "apple", is overwritten by False. You can confirm this by printing the whole dictionary: print(pairs) {'1': 'manzana', 2: 'dos', 1: False, 3: 'pera', 'orange': [2, 3, 4], None: 'True'}
3rd Apr 2019, 12:27 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanx!!!
3rd Apr 2019, 12:41 PM
Rocio Grande
Rocio Grande - avatar