Prev: - | Next: Constructing dictionaries
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
di['GR']
- mapping: a dictionary is usually described as a 'mapping' structure, meaning that it establishes a mapping of keys on values
- collection: a dict is considered as a 'collection' of items in contrast to a list which is considered as a sequence
- indexed: a dictionary is an indexed object with keys as indexes
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
print(di['GR'], di['IT'], di['ES'])
- unordered: a dictionary can not be put in any specific order; ordering/sorting a dictionary is meaningless. Pairs of a dictionary can be written in any order and appear also in any order (when printed) . Ordering (sorting, etc.) pairs in a dictionary is meaningless
di = {'IT':'Italy', 'ES':'Spain', 'GR':'Greece'}
di
- iterable: When used in a for loop a dictionary can return either its keys or key:value pairs to the iterator(s)
# dictionary keys returned to the iterator
di = {'IT':'Italy', 'ES':'Spain', 'GR':'Greece'}
for code in di:
print(di[code], end=' ')
- mutable: dictionary elements can be altered 'in place' (without creating a new copy of a dictionary)
di = {'IT':'Italy', 'ES':'Spain', 'GR':'Greece'}
di['ES'] = 'España'
di['GR'] = 'Hellas'
di
- heterogeneous: dictionary keys should be IMMUTABLE objects (for example: strings or numerics) while values can be of any type
L1 = ['A','B','C','D','E']
di = {'Uno':'Ένα', 1:L1, 20.35:L1[2:4]}
print(di[20.35], di[1], di['Uno'])
di = {1:'uno', 'two':'dos', '3':'tres'}
len(di)
See a pythontutor visualization of the above dict
di = {1:'uno', 'two':'dos', '3':'tres'}
print(di[1], di['two'])
di = {1:[1,2,3], 'two':'2', '3':{'three':'tres'}}
print(di[1], di['3'])
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
print(list(di.keys()))
print(list(di.values()))
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
di['ML'] = 'Malta'
di['FR'] = 'France'
di
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
del di['IT']
di
(like a list)
di = {'1':'one', '2':'two'}
di['1'] = 'uno'
print(di)
# di1 and di2 names reference the same dict object in memory
di1 = {'1':'one', '2':'two', '3':'three'}
di2 = di1
di2['2'] = 'dos'
print(di1)
- a) Only the keys are returned to the iterator with no specific order. Thus the logic of the loop should not be based on any particular order of keys
zoo = {'lion':3,'tiger':2,'elephant':5}
for animal in zoo:
print(animal)
- b) In case we need both keys and values we can call the items() method
# dictionary key:value pair returned to the iterators code and name
di = {'IT':'Italy', 'ES':'Spain', 'GR':'Greece'}
for code, name in di.items():
print(code, 'Country name: ', name)
di = {'IT':'Italy', 'ES':'Spain', 'GR':'Greece'}
for country in di.items():
print('Code = ', country[0], '\tCountry name: ', country[1])
di = {'GR':'Greece', 'IT':'Italy', 'ES':'Spain'}
if 'FR' in di:
print('Country name: ', di['FR'])
else:
print('No such country in di')
. Free learning material
. See full copyright and disclaimer notice