What is a dictionary in python ?
A dictionary in python is a data type ,which works similar to lists ,but they are working nature is complete different. The following example and code snippet will show the creation of the dictionary in python and different methods are supported by dictionary ,among them python dictionaries sort by key is one.
How to define a dictionary?
The above code snippet show the defining the dictionaries in python , unlike lists , dictionaries are created by flower brackets with key value pair separated by colon.
Python Dictionaries Methods
Three methods supported by Dictionaries :
- keys() : To access the Keys in dictionaries.
- values() : To access values associated in Dictionaries
- items() : To display both. keys and values.
The following example illustrate the above methods.
how to sort a dictionary by value in python?
Get() and setdefault() Methods
The get method is used to check the value present or not , it takes two values ,first one is the value and second one is the default value , if the value not present , it return the second value.
The setdefault() method is used to set the value for un defined value. It will work only one time.
Few basics operation in dictionaries is explained , in future post , I will come with more examples.