We have learned about list in previous blogs, list can contain dictionary object vice versa. Both has the ability to change their size at run time. list and dictionary can grow and shrink according to elements at run time. But what is the actual difference b/w these two? Read below.
- One is that elements from list object contains elements in linear manner but the dictionary object contains elements as key and value pair.
- Elements from list object are fetched with element's location but in dictionary the elements is fetched with key.
- Last but not the least that list is an ordered object means when the list object is printed it shows the elements as these were inserted but dictionary sort the keys and print the values.
1 2 3 | dict_ = dict() dict_ = {"Python":"Hunter","Is":"Better"} print(dict_) |
There are lot of methods to manipulate the dictionary object but some of the methods and attributes are removed from python 3.x version. Following script can be used to find the list of available methods and attributes.
1 2 | for i in dir(dict_): print (i) |
Altering Dictionary object:-
1 2 3 | print(dict_["Python"]) dict_["Is"] = "Best" print(dict_) |
It is rational that a good programmer will always keep Modularity to make code readable and loosely couple. there will be some time when we want to map two dictionaries. e.g if we want to use the value of one dictionary object as key to other dictionary object, following simple example will make this concept clear.
1 2 3 4 5 | d1 = {"india":"punjab","usa":"ohio"} d2 = {"asia":"india","west":"usa"} print(d1[d2["asia"]]) |
We can not use list object as key in dictionary, dictionary objects are fetched with key and key needs to be fixed, and as we know from previous post that list can be altered at run time. so if we will use list as key in dictionary and list keeps getting updated every time the program executes then it will be tough for us to get the dictionary object, so that's why python does not allow us to use the list object as dictionary key. but list object can be used as dictionary value.
1 2 | d3 = {[1,2,3]:"value","key2":"value2"} print(d3) |
the above code snippet will give you following error.
TypeError: unhashable type: 'list'
But tuples object can be used as dictionary key as the tuple object can not be altered at run time.
1 2 | d3 = {(1,2,3):"value","key":"value"} print(d3) |
Only unique is allowed in dictionary, if there is any duplicate key in dictionary then the old value of that key will be replaced by new value. See the example.
1 2 | d3 = {"key":"value1","key":"value2","key":"value3"} print(d3) |
As we can see the key is repeated thrice but as the rule says only one unique key in allowed in dictionary so the last value "value3" will be updated in key and all other previous values will be discarded.
Just like list and tuple the dictionary object can also have inner dictionary object and the approach to get the inner dictionary is as same as the list and tuple object.
1 2 3 4 5 | d4 = {"Python":"Hunter","Is":"Great","InnerDicStart":{"innerDicKey":"InnerDictValue"}} print(d4["InnerDicStart"]) print(d4["InnerDicStart"]["innerDicKey"]) |
Operation on Dictionary object:-
1 2 3 4 5 6 7 | d4 = {"Python":"Hunter","Is":"Great","InnerDicStart":{"innerDicKey":"InnerDictValue"}} len(d4) #returns the number of keys print("InnerDicStart" in d4) # returns true if key 'Is' is present. print("nonExsitingKey" not in d4) # true if key is not present in dict |
but key 'innerDicKey' is not visible with 'in' operator, first we have to get the inner dictionary then 'in' operator will work fine.
1 | print("innerDicKey" in d4["InnerDicStart"]) |
Note:- When we try to access non-existing key then python gives us following
keyError 'Your_key'
Like list, tuple and string dictionary objects can also be merged and updated. The only difference in merging dictionary object is that the new keys will be inserted an existing keys will update with new value. e.g.
1 2 3 4 5 6 7 | d5 = {"Python":"Hunter","Is":"good"} d6 = {"and":"cool","Is":"great"} d5.update(d6) print(d5) |
{'Python': 'Hunter', 'Is': 'great', 'and': 'cool'}
Value of key "Is" is update with "great" from d6
It is possible to separately iterate over keys and value. But both methods in are deprecated in Python 3.x but working fine in Python 2.x. If you have Python 2.x installed on your system then the below code will work fine otherwise on Python 3.x it will show errors.
1 2 3 4 5 6 7 8 | #in ptyhon 3.x iterkeys() does not exist. for key in d5.iterkeys(): print (key) #iter over values #in ptyhon 3.x itervalues() does not exist. for key in d5.itervalues(): print (key) |
Converting Dictionary object to list object.
1 2 3 4 5 6 7 8 9 10 11 12 13 | d7 = {"key1":"value1","key2":"value2","key3":"value3"} list_ = d7.items() list_of_keys = d7.keys() list_of_values = d7.values() print(list_) print(list_of_keys) print(list_of_values) |
Converting List object to Dictionary object.
1 2 3 4 5 6 7 8 9 10 | list_1 = ["key1","key2","key3","key4"] list_2 = ["value1","value2","value3","value4"] newDict = dict () newDict = zip(list_1,list_2) print(newDict) # transfor into real dict actualDict = dict (newDict) print(actualDict) |
The over flowing keys or values will be discarded. This is the smartness of python, otherwise it is not good to show error every time.
Comments
Post a Comment
Thanks in anticipation.