Dictionary functions in Python | Dictionary methods in Python

You must first complete Display output in Python 3 | print() function in Python 3 before viewing this Lesson

Dictionary functions in Python | Dictionary methods in Python

1. len()

This method counts and returns number of elements (key:value pairs) in the dictionary.

Syntax

len(<dictionary>)

<dictionary> refers to the user defined dictionary whose length we want to find.

Example 1:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> len(emp)
3

Example 2:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp['subject']='Mathematics'
>>> emp
{'name': 'Munish', 'age': 30, 'designation': 'Teacher', 'subject': 'Mathematics'}
>>> len(emp)
4



2. clear()

clear() method removes all items from the dictionary and the dictionary becomes empty. This method does not delete the dictionary.

Syntax:

<dictionary>.clear()

<dictionary> refers to the user defined dictionary that we want to clear.

Example 1:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp.clear()
>>> emp
{}

To delete the dictionary we can use del statement along with dictionary name.

Example 2:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> del emp
>>> emp
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>  emp
NameError: name 'emp' is not defined

#Error occurs as dictionary emp has been deleted

3. get()

We can use get() method get the item depending upon the key. If key is not present in the dictionary, Python give error or does not display anythin. We can also specify our own message through default argument.

Syntax:

<dictionary>.get(key,[default])

<dictionary> refers to the user defined dictionary whose element we want to get.

key refers to existing key of dictionary whose value we want to get. If key does not exist , Python gives error. If default argument is specified, value at the place of default is displayed.

Example 1:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp.get('age')
30

# emp.get(‘age’) returns 30 as 30 is the value corresponding to key age.

Example 2:

>>> emp.get('job','Key not found')
'Key not found'

# emp.get(‘job’) shows the message ‘Key not found’ as key ‘job’ doesn’t exist in the dictionary.



4. items ()

This method returns all items of dictionary as a sequence of (key,value) tuples. These are returned in no particular order.

<dictionary>.items()

<dictionary> refers to the user defined dictionary.

Example 1:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> T1=emp.items()
>>> T1
dict_items([('name', 'Munish'), ('age', 30), ('designation', 'Teacher')])
>>> for i in T1:
             print(i)

('name', 'Munish')
('age', 30)
('designation', 'Teacher')

Example 2:

As items() function returns a sequences of (key value) pairs, we can write a loop having two variables to access key value pairs as.

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> T1=emp.items()
>>> for k,v in T1:
             print(k,v)

name Munish
age 30
designation Teacher
#for Loop has two iteration variables key,value. For loop iterates through each of the key-value pairs in the dictionary  and assigns keys to loop variable k and values to loop variable v at a time.

5. keys()

This method returns all the keys in the dictionary as a sequence of keys (in form of a tuple). These are returned in no particular order.

Syntax:

<dictionary>.keys()

Example :

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp.keys()
dict_keys(['name', 'age', 'designation'])

6. values ()

This method returns all values from the dictionary as a sequence (a tuple). These are returned in no particular order.

Syntax:

<dictionary>.values()

Example:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp.values()
dict_values(['Munish', 30, 'Teacher'])

7. update ()

This method merges key: value pairs from the new dictionary into the original dictionary. If new dictionary contains same key as original dictionary, key in the original dictionary gets overridden.

Syntax

<dictionary>.updates(<other-dictionary>)

 Example:

>>> emp={'name':'Munish','age':30,'designation':'Teacher'}
>>> emp1={'name':'Rohit','Salary':40000}
>>> emp.update(emp1)
>>> emp
{'name': 'Rohit', 'age': 30, 'designation': 'Teacher', 'Salary': 40000}

Spread the love
Lesson tags: class 12 dictionary in python, combine two dictionaries in python, delete a dictionary in python, Dictionary function in Python, Dictionary methods in Python, get list of keys in dictiionary, get list of values in dictionary, merge dictionaries in python
Back to: Python Programming Tutorial
Spread the love