C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Python

Dictionary Methods in Python

Learn: In this article, we'll going to discuss about various functions and methods associated with dictionaries in python. To append ,update , delete values to the dictionary and toperform other functions and methods to accommodate dictionary values and their keys (with examples).
Submitted by Abhishek Jain, on January 04, 2018

In the previous article, we have discussed about the creation, initialization and accessing python dictionaries. To better understand this please go through the previous article Dictionaries (Creation ,Initialization ,Access) in Python.

1) Append

We can add new values to the already existing dictionary, just by extending it with single pair of values. If we want to add only single element to the dictionary then we write as:

Syntax

Dictionary_name [key]=value  

Example

>>> X={ 'a' :"apple", 'b' :"ball", 'c' :"cat"}
>>> X['d']="dog"
>>> print X

Output

{'a' :'apple' , 'b' :'ball' , 'c' :'cat', 'd' :'dog'}

2) Update

We can also join two dictionaries into one or two dictionaries can be merged in to one by using update() method. Update ( ) merges the keys and values of one dictionary into the other.

It overwrites the value if, both the dictionaries contains the same key.

Syntax

Dictionary_name1.update (Dictionary_name2)

Example

>>> A={'oct':"october",'nov':"november",'dec':"december"}
>>> B={'jan':"january",'feb':"february"}
>>> A.update(B)
>>> print A

Output

{'oct':'october','nov':'november','dec':'december' ,'jan':'january','feb':'february'}

3) Delete

Using 'del' keyword, we can remove element from the existing dictionary.

Syntax

del Dictionary_name[key]

Example

>>> X= { 'a' :"apple", 'b' :"ball", 'c' :"cat",'d' :"dog"}
>>> del X['a']
>>> print X

Output

{'c' :'cat', 'b' :'ball' , 'd' :'dog'}

4) Compare

For comparing or to check whether the two dictionaries are same or not, method cmp( ) is used. It will return three different values for different cases:

  1. If both dictionaries are same, it will return 0.
  2. If first dictionary contains more number of elements than second, then it will return 1.
  3. Otherwise return -1.

Syntax

cmp(dictionary_name1,dictionary_name2)

Example

>>> A={'oct':"october",'nov':"november",'dec':"december"}
>>> B={'jan':"january", 'feb': "february"}
>>> cmp(A,B)

Output

1

5) Length

The length or the number of key-value pairs in the dictionary can be determined by using len( ) method.

Syntax

len(dictionary_name)

Example

>>> X= { 'a' :"apple", 'b' :"ball", 'c' :"cat",'d' :"dog"}
>>> len(X)

Output

4

6) Clear ( )

This method removes all the items from the dictionary.

Syntax

dictionary_name.clear( )

Example

>>> X= { 'a' :"apple", 'b' :"ball", 'c' :"cat",'d' :"dog"}
>>> print X
>>> X.clear( )
>>> print X

Output

{'a' :'apple' , 'b' :'ball' , 'c' :'cat', 'd' :'dog'}
{ }

7) has_key( )

This method is used to identify that the following key k is present in the dictionary or not. Itreturns 'True', if dictionary has a key, otherwise it returns 'False'.

Syntax

dictionary_name.has_key(k )

Example

>>> X= { 'a' :"apple", 'b' :"ball", 'c' :"cat",'d' :"dog"}
>>> X.has_key('a')
>>> X.has_key('z')

Output

True
False


Advertisements




Liked this article? Do share with your friends :)


COMMENTS