Syntax: Below is how we can define a dictionary:
d = {
key: value,
key: value,
.
.
key: value
}
hash() method, should return the hash value. All immutable objects are hashable, but not all hashable objects are immutable.We can also create a dictionary using the dict() function which takes a sequence (list of tuples below for example) of key-value pairs:
d = dict([(key,value),(key,value),...,(key,value)])
Below are some useful dictionary operations:
dict_var[key]: Using the square bracket notation, we can access the associated value for the key.dic_var[key] = value: To add a new key-value pair. Can also be used to update the value for an existing key.del dict_var[key] will delete the specified key-value pair.key in dict_var to test of a key exists in the dictionary.len(dict_var) will return the number of key-value pairs.dict(zip(list1,list2)) will create a dictionary from. two lists. First list of items will be keys for the dictionary, and the second list of items will be the dictionary.key in dict_var check if the key exists.val in dict_var.values() check if the value exists