dic = {"Mandarin orange":2,"Apple":10,"Strawberry":2}
Take out the key
dic.keys()
dict_keys(['Mandarin orange', 'Apple', 'Strawberry', 'melon'])
Retrieve value
dic.values()
dict_values([2, 10, 2, 44])
Extract both key and value
dic.items()
dict_items([('Mandarin orange', 2), ('Apple', 10), ('Strawberry', 2), ('melon', 44)])
Extract both key and value with for statement
for i,k in dic.items():
print(i,k)
Mandarin orange 2
Apple 10
Strawberry 2
Melon 44
Add new keys and values.
dic['Grape'] = 10
dic.items()
dict_items([('Mandarin orange', 2), ('Apple', 10), ('Strawberry', 2), ('melon', 44), ('Grape', 10)])
Recommended Posts