Dictionary are the unordered collection of items.Dictionary has a key:value pair.
In dictionary , an item has a key and its corresponding value.They are enclosed in curly braces.
Dictionary are mutable.
#Initialization of Dictionary:
dict1={} #empty dictionary
dict2={1:"test",2:"rest"} #dictionary with integer keys
dict3={1:"test","name":"rest"} #dictionary with mixed type
dict4= dict({1:"test",2:"rest"}) # dictionary with dict built in function
dict5 = dict([(1,'apple'), (2,'ball')]) #from sequence having each item as a pair
#Accessing value in dictionary:
Unlike list and tuples , dictionary are accessed via key rather than index.
dict2={1:"test",2:"rest"}
print(dict2[2])
#output : rest
dict2={1:"test",2:"rest"}
print(dict2.get(2))
#output : rest
Note : while accessing a key through get() is similar to what we have through square bracket but the difference is when the key is not present.
In such case ,it will return None rather than error.
Let us understand this with an example :
dict2={1:"test",2:"rest"}
print(dict2[3])
#output: print(dict2[3])
KeyError: 3
dict2={1:"test",2:"rest"}
print(dict2.get(3))
#output: None
#Adding or Changing an element in dictionary:
Elements can be added or changed using the assignment operator.
if the element is not present ,it will get added otherwise it get changed.
#updated :
dict2={1:"test",2:"rest"}
dict2[1]= "set"
print(dict2)
#output:{1: 'set', 2: 'rest'}
#Changed :
dict2={1:"test",2:"rest"}
dict2[3]= "set"
print(dict2)
#output : {1: 'test', 2: 'rest', 3: 'set'}
# Removing or deleting item from a dictionary:
There are many ways in which an element can be removed or deleted from a dictionary.
1.)Pop()
2.)popitem()
3.)del()
4.)clear()
Now , Let us go one by one and check each of them with some examples.
#pop() :- In order to remove a particular value from dictionary ,we can go for pop(). It will take key as input and delete the corresponding value.
dict2={1:"test",2:"rest"}
dict2.pop(2)
print(dict2)
#output :- {1: 'test'}
#popitem() :- The popitem() function removes an item from the dictionary arbitarily.
dict2={1:"test",2:"rest"}
dict2.popitem()
print(dict2)
#output :- {2: 'rest'}
#del :- It removes an individual item and also the whole dictionary.
dict2={1:"test",2:"rest"}
del dict2[2]
print(dict2)
#Output :- {1: 'test'}
dict2={1:"test",2:"rest"}
del dict2
print(dict2)
#Output :- NameError: name 'dict2' is not defined
#clear ;- All the items can be removed at once using the clear() method.
dict2={1:"test",2:"rest"}
dict2.clear()
print(dict2)
#Output :- {}
#Dictionary membership test :-
We can test whether a key is available in dictionary or not using the in keyword.
dict2={1:"test",2:"rest"}
print(1 in dict2)
#output :- True
In dictionary , an item has a key and its corresponding value.They are enclosed in curly braces.
Dictionary are mutable.
#Initialization of Dictionary:
dict1={} #empty dictionary
dict2={1:"test",2:"rest"} #dictionary with integer keys
dict3={1:"test","name":"rest"} #dictionary with mixed type
dict4= dict({1:"test",2:"rest"}) # dictionary with dict built in function
dict5 = dict([(1,'apple'), (2,'ball')]) #from sequence having each item as a pair
#Accessing value in dictionary:
Unlike list and tuples , dictionary are accessed via key rather than index.
dict2={1:"test",2:"rest"}
print(dict2[2])
#output : rest
dict2={1:"test",2:"rest"}
print(dict2.get(2))
#output : rest
Note : while accessing a key through get() is similar to what we have through square bracket but the difference is when the key is not present.
In such case ,it will return None rather than error.
Let us understand this with an example :
dict2={1:"test",2:"rest"}
print(dict2[3])
#output: print(dict2[3])
KeyError: 3
dict2={1:"test",2:"rest"}
print(dict2.get(3))
#output: None
#Adding or Changing an element in dictionary:
Elements can be added or changed using the assignment operator.
if the element is not present ,it will get added otherwise it get changed.
#updated :
dict2={1:"test",2:"rest"}
dict2[1]= "set"
print(dict2)
#output:{1: 'set', 2: 'rest'}
#Changed :
dict2={1:"test",2:"rest"}
dict2[3]= "set"
print(dict2)
#output : {1: 'test', 2: 'rest', 3: 'set'}
# Removing or deleting item from a dictionary:
There are many ways in which an element can be removed or deleted from a dictionary.
1.)Pop()
2.)popitem()
3.)del()
4.)clear()
Now , Let us go one by one and check each of them with some examples.
#pop() :- In order to remove a particular value from dictionary ,we can go for pop(). It will take key as input and delete the corresponding value.
dict2={1:"test",2:"rest"}
dict2.pop(2)
print(dict2)
#output :- {1: 'test'}
#popitem() :- The popitem() function removes an item from the dictionary arbitarily.
dict2={1:"test",2:"rest"}
dict2.popitem()
print(dict2)
#output :- {2: 'rest'}
#del :- It removes an individual item and also the whole dictionary.
dict2={1:"test",2:"rest"}
del dict2[2]
print(dict2)
#Output :- {1: 'test'}
dict2={1:"test",2:"rest"}
del dict2
print(dict2)
#Output :- NameError: name 'dict2' is not defined
#clear ;- All the items can be removed at once using the clear() method.
dict2={1:"test",2:"rest"}
dict2.clear()
print(dict2)
#Output :- {}
#Dictionary membership test :-
We can test whether a key is available in dictionary or not using the in keyword.
dict2={1:"test",2:"rest"}
print(1 in dict2)
#output :- True
No comments:
Post a Comment