Dictionary functions and methods
now let's talk about various built in functions and methods provided by Python to manipulate Python dictionaries.
get length of the dictionary - the len() function
to get the length of the dictionary that is the count of the key value pair you can use the len function this function returns length of the dictionary that is not the count of elements key value pair in dictionary who is called the sentence of this method is given below:
len dictionary
accessing items keys and values- get(), items(), keys(), values() methods
the get method
with this method you can get the item with the given key was similar to dictionary key full stop if the key is not present Python by then by default gives error but you can specify your own message through default arguments as per following syntax:
the item method
this method Returns all of the items in the dictionary as a sequence of key value couples. note that these are returned in no particular order
dictionary. Items
the key method
you have already worked with this method to stop this method Returns all of the keys in the dictionary as a sequence of keys inform of a list. know that these are the return in no particular order.
dictionary. Keys
the value method
this method Returns all the values from the dictionary as a sequence a list. not that these are returned in no particular order the syntax to use this method is given below:
Dictionary.values
creating dictionary from keys- the fromkeys method
The fromkeys method is used to create a new dictionary from A sequence containing all the cheese and a common value which will be assigned to all the keys. this method is used as:
dict. Fromkeys key sequence, value
key sequence is a Python sequence containing the key is for the new dictionary.
value is the common value that will be assigned to all the keys if value none is assigned to all the keys.
returns a new dictionary created with the given cheese and common values, which can be stored in a reference name
extend/ update dictionary to the new key value pairs: update() and set default() methods
you can extend or update a dictionary by adding or updating a single key value pair to an existing dictionary or you can add or update key value pairs from another dictionary using the following methods.
dict.setdefault() To insert a new key value pair
dict.update() to update or add key value pairs from a dictionary
1. in the set default method
the set default method inserts a new key value pair only if the key does not already exist. if the key already exists IT returns the current value of the key you can use this method as:
dict. set default ki values
where key and value are the key and value to be added to the dictionary. if value is not passed as input the default null value is used as the value.
returns the value of the new key if and otherwise it will return the value of the existing key.
this method works in two ways:
if the ki given is not already present in the dictionary, it will be add the specified new key value pair to the dictionary and return the value of the added ki.
if the given key is already present in the dictionary in the dictionary that is the existing value of the claim will not be updated, but the current value associated to the specified key is returned.
2.the update method
this method merges key value pairs from the new dictionary into the original dictionary adding or replacing and needed to stop the items in the new dictionary are added to the old one and override and any and terms already there with the same keys.
does Intex to use this method is given below:
dictionary. update other- uh dictionary
making Shallow copy of a dictionary
the copy method of a dictionary creates a Shallow copy of a dictionary. before we discuss copy that as quickly talk about what Shallow copy means
you know that copying something means duplicating it.
1.Shallow copy, where only the upper layer is copied and the inner reference object is not copied.
2. deep copy their all the layers are request will be copied and even in a reference objects are copied
Vishal not go into the details of the copy as to how the deep copy of a Python dictionary is created as it is beyond the scope. we shall stick to the working of BIT copy which creates a Shallow copy of a Python dictionary
the dictionary method copy in used as per the following syntax:
dict. Copy
Deleting elements from dictionaries- clear pop popitem and del
1. the pop method
this method returns the dictionary element associated to past ki. it is used as per the syntax:
dect. Pop ki value
it is the dictionary in which key is to be deleted
ki is the key to be deleted if it is found it is deleted and its value is returned
value is the return value message which will be returned by the method if the given key is not found in the dictionary. if you skip the value and the given key is not found Python will raise error.
the pop method will not only delete the key value pair of mentioned ki but also return the corresponding value
pop method allows you to specify what to display when the given key does not exist rather than the default error message. this can be done as per the following syntax
dictionary. pop ki in case of errors show me
2. the popitem method
start from page 473