September
27
Dictionary
A dictionary, in python, is a built-in data type that represents an unordered collection of key-value pairs. It is also known as an associative array or a hash table in other programming languages. Dictionaries are enclosed in curly braces { } and consist of comma-separated key-value pairs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
tmp_dict = {"key1":"value1", "key2":"value2"} # create a new dictionary with two elements and assign it to the variable tmp_dict dmp_dict # Key1 is a key and its associated value value1 {'key1': 'value1', 'key2': 'value2'} tmp_dict["key1"] # get access by key 'value1' tmp_dict["key2"] 'value2' tmp_dict["key3"] # If key is not there then you get KeyError Traceback (innermost last): File "", line 1, in ? keyError: key3 tmp_dict["key1"]="value3" # modifying value of dictionary dmp_dict {'key1': 'value3', 'key2': 'value2'} dir(tmp_dict) # dir give list of all attributes of object ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] help(tmp_dict) # help generates its document as text on console Help on built-in function iteritems: iteritems(...) D.iteritems() -> an iterator over the (key, value) items of D tmp_dict.keys() # It will give you all the keys in list format ['key1','key2'] tmp_dict.values() # It will give you all the values in list format ['value3','value2'] |
Key Features of Python Dictionaries:
- Mutable: Dictionaries are mutable, which means their contents can be modified after creation. You can add, update, or delete key-value pairs.
- Unordered: Dictionaries are unordered, meaning the order of the key-value pairs is not guaranteed. In other words, the elements are not stored in a specific sequence.
- Unique Keys: Dictionary keys must be unique. If you assign a new value to an existing key, it will update the corresponding value. If you add a new key-value pair with an existing key, it will overwrite the existing value.
- Flexible Key and Value Types: Dictionary keys can be of any immutable type, such as strings, numbers, or tuples. Values can be of any type, including built-in types, custom objects, or even other dictionaries.
1 2 3 |
tmp_dict = {"key1":"value1", "key2":2,"key3":[1,2],"key4":(1,2),"key5":{"key6":"value6"}} tmp_dict {'key3': [1, 2], 'key2': 2, 'key1': 'value1', 'key5': {'key6': 'value6'}, 'key4': (1, 2)} |
Deleting Items From Dictionaries
Dictionary has two method to delete/clear the data
-
-
- del, will delete individual item from dictionary
- clear , will clear all the data and give you a empty dictionary
-
1 2 3 4 5 6 7 8 |
tmp_dict {'key3': 3, 'key2': 2, 'key1': 1} del tmp_dict['key2'] tmp_dict {'key3': 3, 'key1': 1} tmp_dict.clear() tmp_dict {} |