Sitemap

Built-in Dictionary method in python

4 min readNov 3, 2023

--

Hello Everyone Vrushali here again, Today we are going to discuss about the built-in function in Dictionary datatype.

Python has set of built-in methods to work with python dict datatype. Below we’ve explained all the methods you can use with python dir().

We will see the all built in function in dictionary datatype using the dir() function. In below we see how to use the dir() function with dict.

Press enter or click to view image in full size

Here, dir () is a powerful inbuilt function in Python, which returns list of the attributes and methods of any object like functions , modules, strings, lists, dictionaries etc.

let’s start to see one by one built in function in python dict datatype.

1. clear():

clear() is the function in python dict, It will used to clear the dictionary it means clear all element in dict and give the result as a empty dictionary. It clears the dict completely and it does not return anything.

Syntax :

dict. clear ()

Press enter or click to view image in full size

2. copy():

The copy() method returns a shallow copy of the specified dictionary. It will not take a any parameter.

Syntax:

Following is the syntax for copy() method −

dict. copy ()

Press enter or click to view image in full size

3. fromkeys():

Python dictionary method fromkeys () creates a new dictionary with keys from seq and values set to value.

Syntax:

Following is the syntax for fromkeys () method −

dict. fromkeys (seq,value)

Parameters:

  • seq − This is the list of values which would be used for dictionary keys preparation.
  • value − This is optional, if provided then value would be set to this value.
Press enter or click to view image in full size

4. get():

Python Dictionary get() method is used to return the value for key if key is in the dictionary, else key is not in dictionary then it will return default value .

Syntax:

Following is the syntax for get() method −

dict. get (key, default=none)

  • key − This is the Key to be searched in the dictionary.
  • default − This is the Value to be returned in case key does not exist.
Press enter or click to view image in full size

5. items():

Python Dictionary items() is used to print a list of dict’s in (key, value) tuple pairs.

Syntax:

Following is the syntax for items() method −

Get Vrushali Walekar’s stories in your inbox

Join Medium for free to get updates from this writer.

dict.items()

Press enter or click to view image in full size

6. keys():

Python Dictionary keys() methods always returns a list of available dictionary keys. This methos not taking any parameter.

Syntax :

Following is the syntax for keys() method −

dict. keys ()

Press enter or click to view image in full size

7. values():

Python Dictionary values() method always returns a list of dictionary values.

Syntax:

dict.values()

Press enter or click to view image in full size

8. pop():

Python Dictionary pop() method is used to remove specified key and return a corresponding value of it. If the key is not found, then raise a keyerror.

Press enter or click to view image in full size

In above example in dir3 the 4 value is present that’s why it return the value i.e ‘s’ and again print the dir3 then the 4 value get removed. In another example in In[41] dir3 not having a key i.e “w” that’s why it returns a KeyError.

9. popitem():

The popitem() method is used to remove and return a (key, value) pair as a tuple. This pairs are returned in LIFO order. If the dict is empty then raises KeyError.

Press enter or click to view image in full size

10. setdefault():

Python dictionary method setdefault() is similar to get(), but will set dict[key]=default if key is not already in dict.

This method returns the key value available in the dictionary and if given key is not available then it will return provided default value.

Syntax:

Following is the syntax for setdefault() method −

dict.setdefault (key, default = none )

  • key − This is the key to be searched.
  • default − This is the Value to be returned in case key is not found.
Press enter or click to view image in full size

11. update() :

Python dictionary method update() adds dictionary dict2’s key-values pairs in to dict. This function does not return anything.

Syntax:

Following is the syntax for update() method −

dict.update(dict2)

  • dict2 − This is the dictionary to be added into dict.
Press enter or click to view image in full size

Here, we see the some built-in function of dictionary datatype in python. In next article we will see the built-in function in Set datatype.

#vevcodelab

http://vevcodelab.com/

--

--