Method | Description |
---|---|
Used to find the value of the specified key | |
Used to get dictionary with the specified keys and value | |
Used to copy a dictionary | |
Used to get the value of the specified key. If the key does not exist: insert the key, with the specified value | |
Used to update the dictionary with the specified key-value pairs | |
Used to removes all the elements from the dictionary | |
Used to remove the element with the specified key | |
Used to remove the element at the specified position | |
Used to get a list containing the dictionary's keys | |
Used to get a list of all the values in the dictionary | |
Used to get a list containing a tuple for each key value pair |
()
Method
Python Dictionary get() Method return the value for the given key if present in the dictionary.
If not, then it will return None (if get() is used with only one argument).
Example:
dic = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
a = dic.get ("python" )
print (a)
b = dic.get ("hello" )
print (b)
()
Method
The fromkeys() method returns a dictionary with the specified keys and the specified value.
Example:
x = ("a" ,"b" ,"c" )
y = 1
newdict1 = dict .fromkeys (x)
newdict2 = dict .fromkeys (x,y)
print (newdict1)
print (newdict2)
()
Method
Python Dictionary copy() method returns a shallow copy of the dictionary.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
snew = s.copy ()
print (snew)
()
Method
The setdefault() method returns the value of the item with the specified key.
If the key does not exist, insert the key, with the specified value, see example below.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
s1 = s.setdefault ("learn" ,"telugu" )
print (s1)
print (s)
s2 = s.setdefault ("physics" ,"maths" )
print (s2)
print (s)
()
Method
Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
s.update ({"physics" :"maths" })
print (s)
()
Method
The clear() method removes all items from the dictionary.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
print (s)
s.clear ()
print (s)
()
Method
Python dictionary pop() method removes and returns the specified element from the dictionary.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
print (s)
s.pop ("hello" )
print (s)
()
Method
Python dictionary popitem() method removes the last inserted key-value pair from the dictionary and returns it as a tuple.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
print (s)
s.popitem ()
print (s)
s.popitem ()
print (s)
()
Method
The keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
key = s.keys ()
print (key)
s["physics" ] = "maths"
print (key)
()
Method
values() is an inbuilt method in Python programming language that returns a view object.
The view object contains the values of the dictionary, as a list.
If you use the type() method on the return value, you get “dict_values object”. It must be cast to obtain the actual list.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
val = s.values ()
print (val)
s["physics" ] = "maths"
print (val)
s["learn" ] = "telugu"
print (val)
()
Method
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.
In Python Dictionary, items() method is used to return the list with all dictionary keys with values.
Example:
s = {
"hello" :"world" ,
"python" :"program" ,
"learn" :"python"
}
item = s.items ()
print (item)