What is Dictionary in Python with examples?
Dictionary
A dictionary is a mixed collection of elements. Unlike other collection data types such as a list or tuple, the dictionary type stores a key along with its element. It also a mutable data type.
.The keys in a Python dictionary are separated by a colon ( : ) while the commas work as a separator for the elements. The key-value pairs are enclosed with curly braces { }.
Syntax of Dictionary:
Dict_Name = {key_1:val_1, key_2:val_2,key_3:val_3}
Accessing values in the dictionary
To access values in dictionary, square brackets are used along with the key to get values
Example 1: #single values
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict[name])
- Iterathon
- >>>print(Dict[subject])
- Blog
Example 2: #All values
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.values())
- dict_values([‘Iterathon’, ‘blog’])
Accessing keys in the dictionary
To access keys in dictionary, use one magical word called keys().
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.keys())
- dict_keys([‘name’, ‘subject’])
Some important operations in python
- len()
- clear()
- copy()
- items ()
- keys()
- values()
- get()
- setdefault()
- del
len()
len() returns the length of the dictionary that is total pairs (keys and values)
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(len(Dict)
- 2
clear()
clear() is used to delete all elements in the dictionary. it returns a empty dictionary.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.clear())
- None
- >>>print(Dict)
- {}
copy()
copy() returns a original duplicate of the dictionary.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>dict = (Dict.copy())
- >>>print(dict)
- {‘name’:’Iterathon’, ‘subject’:’Blog’}
items()
items() returns whole dictionary that is both key and value pairs.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.items())
- dict_items([(‘name’, ‘Iterathon’), (‘subject’, ‘Blog’)])
keys()
keys() is used to returns a keys in the dictionary.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.keys())
- dict_keys([‘name’, ‘subject’])
values()
values() is used to returns the values in the dictionary.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.values())
- dict_values([‘Iterathon’, ‘Blog’])
get()
get() returns the value for the key passed as arguments. If the key is not present, It returns None.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>print(Dict.get(‘name’))
- Iterathon
- >>>print(Dict.get(‘Iterathon’))
- None
setdefault()
setdefault() is used to sets a default value for keys that not present in the dictionary.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>dict = Dict.setdefault(“motive”,”Learn Python Easy”)
- >>>print(dict)
- Learn Python Easy
del
del is used to deletes a entire dictionary including spaces. After deletion, you tried to call that the dictionary, It led to thrown an error.
Example:
- >>>Dict = {‘name’:’Iterathon’, ‘subject’:’Blog’}
- >>>del Dict
- >>>print(Dict)
- NameError: name ‘a’ is not defined
My Previous Post
What is Set Operation: https://iterathon.tech//what-is-set-operation-in-python-with-example/
LEARN LITTLE BY LITTLE TO CHANGE THE TECH WORLD