Professional Documents
Culture Documents
DICTIONARY
Dictionary
• It is another collection in Python but with different in way of storing and
accessing. Other collection like list, tuple, string are having an index
associated with every element but Python Dictionary have a “key” associated
with every element. That’s why python dictionaries are known as
KEY:VALUE pairs.
• Dictionary is mutable. i.e, we can modify its contents(values). But, Key must
be unique and its immutable.
Dictionary
• Like with English dictionary we search any word for meaning associated with
it, similarly in Python we search for “key” to get its associated value rather
than searching for an index
{‘key’:’value’}
Characteristics
• Unordered set
• Not a sequence
• Indexed by Keys, Not Numbers
• Keys must be unique
• Mutable
• Internally stored as Mappings
Creating Dictionary
o Dictionary elements must be between curly brackets { }
o Each value must be paired with key element
o Each key-value pair must be separated by comma(,)
dictionary_name = {key1:value,key2:value,....}
Creating Dictionary
• EMPTY DICTIONARY
D = {} Or D = dict()
• We can check the existence of key in dictionary using “in” and “not in
• If given key exists in dictionary then in operator will return true, false otherwise
>>>l= list(my_dict.keys())
>>>l
['empno', 'name', 'dept', 'salary']
values()
values() : this method return all the values in the dictionary as a sequence of
values(not a list form)
> > > mydict={'empno':1,'name':'Shivam','dept':'sales','salary':25000}
>>>mydict.values() d1={'a': 'apple', 'b': 'ball', 'c': 'cat',
'd': 'dog'}
dict_values([1, 'Shivam', 'sales', 25000])
for key in d1.values():
print(key)
items()
items() : this method return all the values in the dictionary as a list of tuples
(key — value) pair
> > > mydict={'empno':1,'name':'Shivam','dept':'sales','salary':25000}
>>>mydict.items()
dict_items([('empno', 1), ('name', 'Shivam'), ('dept', 'sales'), ('salary', 25000)])
len()
len() : it return the length of dictionary i.e. the count of elements (key:value
pairs) in dictionary
d1[‘key’] = d1.get(‘key’)
update()
update() merges the key:value pair from the new dictionary into original
dictionary, adding or replacing as needed. The items in the new dictionary are
added to the old one and override items already with the same keys
d1 = {'a': 'apple', 'b': 'boy', 'c': 'cat', 'd': 'dog'}
d2= {‘b’:’ball’,’c’:’cow’,’e’:’elephant’}
d1.update(d2) {'a': 'apple',
‘b’:’ball’,’c’:’cow’,’d’:’dog’,’e’:’elephant’}
print(d1)
min() & max()
• max() : this function return highest value in dictionary, this will work only if all the
values in dictionary is of numeric type
• min() :this function return lowest value in dictionary, this will work only if all the values
in dictionary is of numeric type.
my_dict = {1:500, 2:600, 4: 560}
print('Maximum Value: ',max(my_dict))
print('Minimum Value: ',min(my_dict))
print('Maximum Value: ',max(my_dict.values()))
print('Minimum Value: ',min(my_dict.values()))
sorted()
sorted() : this function is used to sort the key or value of dictionary in either
ascending or descending order. By default it will sort the keys.
sorted()
sorted() : this function is used to sort the key or value of dictionary in either
ascending or descending order. By default it will sort the keys.
Program to create dictionary from user input
Output
Create a dictionary by counting characters from
a string