Dictionary
Dictionary in Python is an unordered collection of data values, used to store data values like a dictionary which unlike other data types that hold only single value as an element. Dictionary holds Key: Value pair. Each Key-Value pair in a dictionary is separated by colon ( : ), whereas each key is separated by 'comma'.The dictionary is defined into element Keys and values.
- Keys must be a single element
- Value can be any type such as list, tuple, integer, etc.
Creating Dictionary
In Python a dictionary can be created by placing a sequence of elements within curly braces {}, separated by 'comma'. Values in a dictionary can be of any data type and can be duplicated, whereas keys can not be repeated and must be immutable.
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces {}.
Syntax
- Dict = {"Name": "technicalgupta", "Age": 22}
Example
# creating empty dictionary
dict={}
print(dict)
# creating a dictionary with integer keys
dict={1:'btech',2:'second',3:'year'}
print(dict)
# creating a dictionary with mixed keys
dict={'name':'technicalgupta',1:[1,2,3,4]}
print(dict)
# with dict() method
dict=dict({1:'btech',2:'second',3:'year'})
print(dict)
Output
{ }
{1:'btech',2:'second',3:'year'}
{'name':'technicalgupta',1:[1,2,3,4]}
{1:'btech',2:'second',3:'year'}
Note : Dictionary keys are case sensitive, same name but different cases of key will be treated distinctly.
Accessing the dictionary elements
In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets. There is also a method called get() that will also help in accessing the element of a dictionary.
Example
# creating a dictionary
Dict={1:'technicalguptaji','name':'sunny',3:'gupta'}
#accessing element using key
print(Dict['name'])
#accessing element using get() method
print(Dict.get(3))
Output
sunny
gupta
Adding elements into a dictionary
One value at a time can be added to a dictionary by defining value along with the key. like Dict[key]='value'. Updating an existing values in a dictionary can be done by using the built-in update() method.
Note : While adding a value ,if the key value already exists, the value gets updated otherwise a new key with the value is added to the dictionary.
Example
-
- Dict = {}
- print("Empty Dictionary: ")
- print(Dict)
-
-
- Dict[0] = 'technical'
- Dict[2] = 'gupta'
- Dict[3] = 'ji'
- print("Dictionary after adding 3 elements: ")
- print(Dict)
-
- # Updating existing Key's Value
- Dict[3] = 'class'
- print("Updated key value: ")
- print(Dict)
Output
Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'technical', 2: 'gupta', 3: 'ji'}
Updated key value:
{0: 'Peter', 2: 'Joseph', 3: 'class'}
Removing elements from Dictionary
In Python dictionary deletion of keys can be done by using the del keyword. using del keyword, specific values from a dictionary as well as whole dictionary can be deleted. Other function like pop() and popitem() can also be used for deleting specific values and arbitrary values from a dictionary. All the items from the dictionary can be deleted at once by using clear() method.
Example
# Initial dictionary
Dict={5:'welcome',6:'to',7:'technical',8:'guptaji'}
print(Dict)
# Deleting a key value
del Dict[6]
print(Dict)
# Deleting a key using pop() method
Dict.pop(5)
print(Dict)
# Deleting entire dictionary
Dict.clear()
print(Dict)
Output
{5:'welcome',6:'to',7:'technical',8:'guptaji'}
{5:'welcome',7:'technical',8:'guptaji'}
{7:'technical',8:'guptaji'}
{}
No comments:
Post a Comment