Reputation: 1
I'm new in Python and I'm trying to print a dictionary that I made:
print({
"Mamifero":"Gato",
"Reptil":"Lagartija",
"Mamifero":"Perro",
"Reptil":"Tortuga",
"Reptil":"Serpiente",
"Mamifero":"Koala"
})
But the Windows console only gave me this:
{'Mamifero': 'Koala', 'Reptil': 'Serpiente'}
How do I do to see all the elements using print()?
Upvotes: 0
Views: 755
Reputation: 1
Thanks everyone, I'm learn something today and I create this as an example of a Dictionary:
dictionary_1 = {"animal":"gato", "numero_patas":4, "cola":True, "edad":3.6, "nombre":None}
print(dictionary_1)
for key, value in dictionary_1.items():
print(key, "=", value)
And the Windows Console gave me this:
{'animal': 'gato', 'numero_patas': 4, 'cola': True, 'edad': 3.6, 'nombre': None}
animal = gato
numero_patas = 4
cola = True
edad = 3.6
nombre = None
Thank you very much.
Upvotes: 0
Reputation: 166
You can use the function .items()
, this returns the key
and value
in a list
of tuples
. But first define your dictionary.
Example:
dic = {'a' : 1, 'b' : 2, 'c' : 3 , 'd' : 4}
print(dic.items())
Upvotes: 1
Reputation: 472
Your problem actually bypassing the need for unique keys in the dictionary. As discussed in the thread Make a dictionary with duplicate keys in Python, the most convenient solution might be in creating custom class as following:
class p(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
def __str__(self):
return self.name
d = {p('Mamifero'): 'Gato', p('Reptil'): 'Lagartija', p('Mamifero'): 'Perro', p('Reptil'): 'Tortuga', p('Reptil'): 'Serpiente', p('Mamifero'): 'Koala'}
print(d)
Otherwise, the thread provides many similar solutions. This might be the easiest to reproduce, though.
Upvotes: 1
Reputation: 1
First, a dictionary should have distinct keys. After you fix that, I suggest you check out dictionaries' functionalities below:
Input:
# First_Name: [age, job, eyes]
peoples_details = {
"Tom":[25, "Lawyer", "Brown"],
"Adam":[28, "Python Specialist", "Blue"],
"John":[45, "Unemployed", "Green"]
}
for value in peoples_details.values():
print(value)
for you_can_name_this_var_anything_actually in peoples_details.keys():
print(you_can_name_this_var_anything_actually)
for key, value in peoples_details.items():
print(key, value)
Output:
====================== RESTART: C:/Users/tom/Desktop/py.py =====================
[25, 'Lawyer', 'Brown']
[28, 'Python Specialist', 'Blue']
[45, 'Unemployed', 'Green']
Tom
Adam
John
Tom [25, 'Lawyer', 'Brown']
Adam [28, 'Python Specialist', 'Blue']
John [45, 'Unemployed', 'Green']
Upvotes: 0