Python Dictionaries

Similar to other various high level languages python also provide facility to store data in key value pair style. Dictionary is one of the most used collection in python amongg others such as Tuple, Lists and Set .The major charcteristic of dictionary is that, it does not allow duplicate data items. The other fetures of dictionaries are as.

  • Ordered
  • Changeable

In previous versions of the python dictionaries were also unordered. But after the release of version 3.7 the dictionaries are treated as ordered data sets.

Dictionary Example:

py_dict = {
  "rollno": 10,
  "name": "Jack",
  "Program": "MCS"
}
print(py_dict )

Access Dictionary Items:

We can easily access any single item from the dictionary by using its key value. From the above example if we want to print name then except the entire data values we will write it as.

print(py_dict ["name"])

Changeable Property:

Python changeable property means we can easily add or remove data values from dictionary after its creation.

Duplicate Behaviour:

In python dictionaries whenever we will try to add duplicate key value, it will overwrite the previous data value. As a result each key value will contain single data item. This property is called duplicate is not allowed. This property is also referred as atomic value constrain.

Example:

py_dict = {
  "rollno": 10,
  "name": "Jack",

  "name": "Herry",
  "Program": "MCS"
}
print(py_dict )

Output:

In the above example name key will overwrite the existing value as of "Jack". In similar way if we use the same key any no of time the existing data value will be wiped and new stored. Normally this is not a good practice in python to assign multiple data values to the same key index.

How to find length of Dictionary:

In python programming there is an easy and simple way of getting the length of various data sets. We have to just use len() built in function to get the length of dictionary. Len function will automatically skip counts of duplicate key values. It will provide an integer number of uniques dictionary keys.

Example:

print( len(py_dict ) )

This will print 3 for both examples in this topic. Either the second example containing 4 values but there is a key name is used twice and this will skipped in counting.

 Dictionary items type:

There may be any type of data items in dictionary. Which may include:

  1. Number
  2. String
  3. Boolean
  4. Lists

 Update Dictionary item

We can easily update any key value from the python dictionary. For this purpose just provide the key value and the identifier of the dictionary and then use assignment operator to update key value.

py_dict ["name"] = "Alexander"

This will update key value name.

Add item in Dictionary:

To add new item or data avlue in dictionary we just have to specify new key value and its data. The assignment operator will automatically add new key in dictionary and store new data item in it.

Example:

py_dict ["age"] = 19

Now the py_dict dictionary will include four keys named as rollno, name, program and age. In the similar way we can insert multiple data items of various data types in dictionary.

Remove Dictionary Elements:

To remove the entire elements we use clear method of dictionary. After removal of all elements we can add new data items in the same dictionary.

Example:

py_dict.clear()

Delete Dictionary:

By using del command we can delete the entire dictionary along with its complete elements.

Remove Single Element:

In the similar way as deleting entire dictionary, we can also remove single data item fromt he dictionary by specifying its key value. If we provide key value with dictionary name then only the specified key and its item will be removed if exist.

Useful Functions for Dictionary:

Sr. Function Example Description
1 len() len(py_dict) This function will return an integer value indicating the total number of items in dictionary.
2 str() str(py_dict) str or can say string function will convert the entire ditionary into printable form.
3 type() type(py_dict) This will print tpe of the dictionary.
4 cmp() cmp(py_dict1,py_dict2) Compare function is used to compare data items of the provided dictionaries.

Dictionary Methods:

Sr. Method Use Description
1 copy() py_dict.copy() It will return shallow copy of called dictionary.
2 has_key(key) py_dict.has_key("name") It will return boolean. In this exaplei it will return TRUE. because py_dict has name key.
3 update(py_dict2) py_dict.update(py_dict2) This method will add key values of py_dict2 to the py_dict1
4 keys() py_dict.keys() keys method will return the complete list of the dictionary.
5 values() py_dict.values() Similar to the previous method, This mehod will return list of data items.
6 clear() py_dict.clear() Clear method remove all the elements of the dicitonary.
7 get() py_dict.get("abc",default="NA") This method is used to get particular key value with default parameter. In this example as our py_dict dictionary does not include abc key so that it will return NA.
8 fromkeys() py_dict.fromkeys() This method will create a new dictionary.
9 items() py_dict.items() This method will return both keys and values in tuple pair.
10 setdefault() py_dict.setdefault("abc","Your Name") This will set the default value for the specified key.
Comments
Login to TRACK of Comments.