EMAIL SUPPORT
dclessons@dclessons.comLOCATION
AFDictionaries Deep-Dive
What is Dictionaries
It contains the group of element arranged in key-Value pairs. The Structure of Dictionary is, it has two value, first one is Key and second one is its related Value. The Key and its value are separated by colon ( : ) . And All the Key and its value are then inserted under {}.
Example:
Dict = {‘Company’: ‘Dclessons’, ‘year’: 2017, ‘Country’: ‘NZ’}
Here Keys are Company, Year, Country and its related values are Dclessons, 2017, NZ.
Program1: Write a Python Program to create a dictionary and display its values.
Working Operation on Dictionaries
Here we can see what other option we can do to operate Dictionaries.
Use of Len()
We can use len() function to get the number of Key-Values which are there in a Dictionary.
portal = {"Company":"Dclessons", "year":2017, "Country":"NZ"}
num = len(portal)
Printing Num will discplay , that there are 3 Key-Values item present in this.
Use of Modify()
Using modify() function , we can modify the existing values of a key to new values.
Portal[“year”] = 2016
This will Modify Year value from 2017 to 2016.
Inserting a new Key Value
We can also insert a new key value in to existing Dictionary. This can be done by below method.
Portal [“Platform”] = online
Once we do and print this value, we will get following output.
Portal = {"Company”: Dclessons", "year":2017, “Platform”: “Online”, "Country":"NZ"}
Here we can well see that it is not inserted in last of existing pairs. However it may be added at any place in dictionary.
Use of del()
We can also delete the Key value from Dictionary, by use of del () function.
del portal[“year”]
In & Not In Function:
With use of in we can find that, weather a key value is present in that particular Dictionary, if it is present it will return TRUE else it will return FALSE.
We should not use duplicate key value in a Dictionary, if we do so, it will replace the last value to previous value.
Let’s see in the example
Portal = {"Company”: Dclessons", "year":2017, “Platform”: “Online”, "Country":"NZ" , “year”:2018}
Now when we print this we will see below output
Portal = {"Company”: Dclessons", "year":2018, “Platform”: “Online”, "Country":"NZ"}
Below are some methods, which can be used in Dictionary.
- Clear (): Remove all key values from list
- Copy (): Copy all element from a dictionary to new Dictionary.
- Get (a, [b]): Return the value associated with ‘a’ if ‘a’ is not found it will return ‘b’
- Items (): Return an object that contain key value pairs of dictionary
- Keys (): Return a sequence of keys from a Dictionary
- Values (): Return a sequence of values from Dictionary
- Update (): Adds all elements from a Dictionary to new dictionary
Program 2: Write a Python program to get keys, values and key-Values pairs from a Dictionary.
program3: Write a python program to create a Dictionary and find its sum
Program 4: Write a Python Program to create a Dictionary from keyboard and display the element.
How to use Loop in Dictionary
LEAVE A COMMENT
Please login here to comment.