In this section, I’m going to be discuss the Dictionary class. Dictionary object, is basically a mapping, or an associative array of keys and their associated values. Keys within dictionaries have to be unique and immutable type objects like numbers and strings. First of all, if you want to create a dictionary object, you can use the dictionary function to create that object. So empty_dict is going to be a empty dictionary object. And we can see, when that gets printed, that a empty dictionary is represented as a pair of curly braces.
# The dict class allows creating an associative array # of keys and values. Keys must be unique immutable objects. empty_dict = dict() print('empty_dict ->',empty_dict) empty_dict = {} print('empty_dict ->', empty_dict) dict_syn= {'k1': 'v1', 'k2': 'v2'} print('dict_syn ->', dict_syn) dict_syn =dict(k1='v1', k2='v2') print('dict_syn ->', dict_syn) print("dict_syn['k2'] ->", dict_syn['k2']) dict_syn['k3'] = 'v3' print('dict_syn ->', dict_syn) del(dict_syn['k3']) print('dict_syn ->',dict_syn) dict_syn['k1']=1 print('dict_syn ->', dict_syn) dict_syn['k2']=1 print('dict_syn ->', dict_syn) dict_ref = dict_syn dict_copy = dict_syn.copy() dict_syn.clear() print('dict_syn ->',dict_syn) print('dict_ref ->', dict_ref) print('dict_copy ->',dict_copy) key_list = dict_copy.keys() print('key_list ->', key_list) value_list = dict_copy.values() print('value_list ->', value_list) mapping = zip(key_list, value_list) print('mapping ->', mapping) dict_new = dict(mapping) print('dict_new ->', dict_new) mapping =dict_new.items() print('mapping ->', mapping) dict_same_value =dict.fromkeys(key_list, 0) print('dict_same_value ->', dict_same_value) value1 = dict_new['k1'] print('value1 ->', value1) value1 =dict_new.get('k1') print('value1 ->', value1) default_value =dict_new.get('non-existent', 0) print('default_value ->', default_value) value2 = dict_new.pop('k2') print('value2 ->', value2) print('dict_new->', dict_new) default_value = dict_new.pop('non-existent', 0) random_item = dict_new.popitem() print('random_item ->', random_item) print('dict_new ->', dict_new) print("'k3' in dict_new ->", 'k3' in dict_new) value3 = dict_new.setdefault('k3', 3) print('value3 ->',value3) print("'k3' in dict_new ->", 'k3' in dict_new) print('dict_new->', dict_new) value3 = dict_new.setdefault('k3', 0) print('value3 ->',value3) print('dict_new ->', dict_new) The Output : empty_dict -> {} empty_dict -> {} d ict_syn -> {'k1': 'v1', 'k2':'v2'} dict_syn -> {'k1': 'v1', 'k2': 'v2'} dict_syn['k2'] -> v2 dict_syn-> {'k1': 'v1', 'k3': 'v3', 'k2': 'v2'} dict_syn -> {'k1': 'v1', 'k2':'v2'} dict_syn -> {'k1': '1', 'k2': 'v2'} dict_syn -> {'k1': '1', 'k2':'1'} dict_syn -> {} dict_ref -> {} dict_copy {'k1': 1, 'k2': 1} key_list-> dict_keys(['k1', 'k2']) value_list -> dict_values([1, 1]) mapping ->dict_new -> {'k1': 1, 'k2': 1} 'k3' in dict_new ->False 'k3' in dict_new -> True
If you want to create an empty dictionary. You can see, in this example, empty_dict is assigned the pair of empty curly braces, which also makes an empty dictionary object. And so that got printed out there. The typical dictionary syntax, if you want to create multiple keys and multiple values, is to use the curly braces and then have your key value, colon, and then your value associated with that key; comma, your next key, colon, and then the next value associated with that key. And on and on this could go, with comma separated key and value pairs. So you can see, if we go to print out the dict_syn that was created, it shows those different keys and values much in the same way that we actually enter them. Another dictionary syntax you can use, is with the dictionary function where, in the dictionary function, you can do a key equals its value, comma, the key equals its value, comma, and so on.
empty_dict = {} print('empty_dict ->', empty_dict) dict_syn= {'k1': 'v1', 'k2': 'v2'} print('dict_syn ->', dict_syn) dict_syn =dict(k1='v1', k2='v2') print('dict_syn ->', dict_syn) print("dict_syn['k2'] ->", dict_syn['k2']) The output : empty_dict -> {} dict_syn -> {'k1': 'v1', 'k2': 'v2'} dict_syn -> {'k1': 'v1', 'k2': 'v2'} dict_syn['k2'] -> v2
If you want to add a value to an existing dictionary, you use that same syntax that you used to reference a value, but then you assign a new value. So in this case, the dictionary is going to get a new key called k3, which will have a value v3. And we can see, when we go to print that whole dictionary again, now that k3 value, or that now that k3 key and that value v3, are in the dictionary. Now dictionaries are not normally ordered objects, although there is a class called ordered_dict that is available. But the basic dictionary is not going to have any particular order. It’s based upon the hashed value of the different keys. So we saw a way to add an element into the dictionary. Using del() function, you can delete an element that’s in the dictionary. In this case, from the dictionary we’re deleting k3. That gets rid of, not only k3 the key, but also its value. And so, after having done that, you can see that that k3 key and value v3 are no longer present in the dictionary.
dict_syn['k3'] = 'v3' print('dict_syn ->',dict_syn) del(dict_syn['k3']) print('dict_syn ->', dict_syn) The output : dict_syn -> {'k1': 'v1', 'k3': 'v3', 'k2': 'v2'} dict_syn -> {'k1':'v1', 'k2': 'v2'}
If you want to change the value of an existing item in the dictionary using the dictionary name, square bracket, the key, square bracket, equals the value. Just like, when adding a new value, you can change an existing value. This also illustrates the point that keys must be unique, but values do not have to be unique. Okay, so next we see dictionary, next we see dict_ref is assigned equal to dict_syn. This creates a reference to dict_syn, whereas dict_copy creates a separate distinct copy using the copy() method of the dictionary object.
dict_ref = dict_syn dict_copy = dict_syn.copy() dict_syn.clear() print('dict_syn ->', dict_syn) print('dict_ref ->',dict_ref) print('dict_copy ->', dict_copy)
If you take a list of keys and a list of values, essentially we only have to do one more thing and you create a dictionary. Using the zip() function with a list of keys and a list of values, creates what they call a mapping object. And we see that mapping, or zip, object. And then, using the dictionary function applied to that mapping, or zip, object, actually creates our new dictionary. So those can be very helpful methods when working with dictionaries. Finally, we have some inspection functions. If you want to test, to see whether a value is in a dictionary or not in a dictionary, you can see, you can test for, the presence of the key in the dictionary name. If the key is not there, then it will return a value of False. Of course, when the key is not there, not in for that dictionary, it will return a value of true. So now you know a little bit about how to work with dictionaries in Python.
mapping =zip(key_list, value_list) print('mapping ->', mapping) dict_new =dict(mapping) print('dict_new ->', dict_new) print('dict_new ->,dict_new) print (" 'k3' in dict_new ->", 'k3' in dict_new) print (" 'k3' in dict_new ->", 'k3' not in dict_new) The output : mapping ->dict_new -> {'k1': 1, 'k2': 1} 'k3' in dict_new ->False 'k3' in dict_new -> True