മലയാളത്തിൽ
▪ The Python dictionary is an unordered collection of items or elements.
▪ All other compound data types in Python have only values as their elements or items
whereas the dictionary has a key: value pair. Each value is associated with a key.
▪ In the list and the tuple, there are indices that are only of integer type but in dictionary,
we have keys and they can be of any type.
▪ Dictionary is said to be a mapping between some set of keys and values. Each key is
associated to a value.
▪ The mapping of a key and value is called as a key-value pair and together they are
called one item or element.
▪ A key and its value are separated by a colon (:) between them.
▪ The items or elements in a dictionary are separated by commas and all the elements
must be enclosed in curly braces.
▪ A pair of curly braces with no values in between is known as an empty dictionary.
▪ The values in a dictionary can be duplicated, but the keys in the dictionary are unique.
▪ The values in a dictionary can be of any data type, but the keys must be of
immutable data types (such as string, number or tuple).
▪ Empty Dictionary
Method-2 using dict()
▪ >>> dict1 = {}
# empty dictionary
▪ >>>print dict1
dic= dict()
▪ {} # Output
Dictionary with integer keys
▪ Dictionary with integer keys
>>>d1= dict({1:’red’, 2:’yellow’,3:’green’})
▪ >>> dict1 = {1:’red’,2:’yellow’,3:’green’}
▪ >>>print dict1
▪ {1: ‘red’, 2: ‘yellow’, 3: ‘green’} # Output
▪ Dictionary with mixed keys
▪ >>> dict1 = {‘name’ : ‘jinnie’, 3:[‘Hello’,2,3]}
▪ >>>print dict1
▪ {3: [‘Hello’, 2, 3], ‘name’: ‘jinnie’} # Output
▪ In order to access the elements from a dictionary, we can use the value of the key
enclosed in square brackets.
▪ Python also provides a get() method that is used with the key in order to access the
value.
▪ >>> dict1 = {‘name’ : ‘John’, ‘age’ : 27
▪ >>> dict1[‘name’]
▪ ‘John’ # Output
▪ >>> print (dict1[‘age’])
▪ 27 # Output
▪ >>> dict1.get(‘name’)
▪ ‘John’ # Output
▪ >>> dict1.get(‘age’)
▪ 27 # Output
▪ Dictionaries in Python are mutable.
▪ If the key is present in the dictionary, then the associated value with that key is
updated or changed; otherwise a new key: value pair is added.
▪ >>> dict1 = {‘name’ : ‘John’, ‘age’ : 27}
▪ >>> dict1[‘age’] = 30 # updating a value
▪ >>> print(dict)
▪ {‘name’: ‘John’, ‘age’: 30 } # Output
▪ >>> dict1[‘address’] = ‘Alaska’ # adding a key: value
▪ >>>print(dict1)
▪ {‘name’: ‘John’, ‘age’: 30, ‘address’: ‘Alaska’} # Output
▪ The items or elements from a dictionary can be removed or deleted by using pop()method.
▪ pop()method removes that item from the dictionary for which the key is provided. It also
returns the value of the item.
▪ popitem()method is used to remove or delete and return an arbitrary item from the dictionary.
>>>dict_cubes = {1:1, 2:8, 3:9, 4:64, 5:125, 6:216}
>>>dict_cubes.pop(3) # remove a particular item
9 # Output
>>>dict_cubes
{1: 1, 2: 8, 4: 64, 5: 125, 6: 216} # Output
>>>dict_cubes.popitem() remove an arbitrary item
(1, 1) # Output
>>>dict_cubes.popitem()
(2, 8) # Output
>>>dict_cubes
{4: 64, 5: 125, 6: 216} # Output
▪ del() method can be used to delete an item
▪ This can be used to delete the dictionary itself. When this operation is performed, the
dictionary is deleted from the memory and it ceases to exist
▪ del dict_cubes[6] # delete a particular item
▪ >>>dict_cubes
▪ {4: 64, 5: 125} # Output
▪ The clear() method removes all the items or elements from a dictionary at once. When
this operation is performed, the dictionary becomes an empty dictionary.
▪ >>>dict_cubes.clear() # remove all items
▪ >>>dict_cubes
▪ {} # Output
▪ >>>del dict_cubes # delete the dictionary itself
▪ >>> print (dict_cubes)
▪ NameError: name ‘dict_cubes’ is not defined
▪ 1. Traversing
▪ Traversing in dictionary is done on the basis of keys because they are unique.
▪ For this, for loop is used, which iterates over the keys in the dictionary and
prints the corresponding values using keys
▪ dict = {1:’a’,2:’b’,3:’c’,4:’d’}
for c in dict
print(c,dict[c])
Output:
1a
2b
3c
4d
▪ 2. Membership
▪ Using the membership operator (in and not in), we can test whether a key is in
the dictionary or not
▪ It takes an input key and finds the key in the dictionary. If the key is found, then
it returns True, otherwise, False
▪ >>>cubes = {1:1, 2:8, 3:27, 4:64, 5:125, 6:216}
▪ >>>3 in cubes
▪ True # Output
▪ >>>7 not in cubes
▪ True # Output
▪ >>>10 in cubes
▪ False # Output
▪ len(dict): It returns the number of items (length) in the dictionary.
▪ sorted(dict) :It returns the sorted list of keys.
▪ str(dict): It produces a printable string representation of the dictionary.
▪ dict.copy() :It returns a copy of the dictionary.
▪ dict.get(key, default=None) :For key key, returns value or default if key not in
dictionary.
▪ dict.has_key(key): It finds the key in dictionary; returns True if found and false
otherwise.
▪ dict.items() :It returns a list of entire key: value pair of dictionary.
▪ dict.keys() :It returns the list of all the keys in dictionary.
▪ dict.values(): It returns all the values in the dictionary