Google News
logo
Python Dictionary
Creating a dictionary is as simple as placing items inside curly braces {} separated by comma.

An item has a key and the corresponding value expressed as a pair, key: value.

A dictionary is a data type similar to arrays, but works with keys and values instead of indexes. Each value stored in a dictionary can be accessed using a key, which is any type of object (a string, a number, a list, etc.) instead of using its index to address it.

For example, a database of phone numbers could be stored using a dictionary like this :
>>> phonebook = {}
>>> phonebook["Chanti"] = 9966463846
>>> phonebook["Ramana"] = 9963666068
>>> phonebook["F T L"] = 9010023210
>>> print(phonebook)
{'Chanti': 9966463846, 'Ramana': 9963666068, 'F T L': 9010023210}
>>> 
Alternatively, a dictionary can be initialized with the same values in the following notation :
>>> phonebook = {
	"Chanti" : 9966463846,
	"Ramana" : 9963666068,
	"F T L" : 9010023210
	}
>>> print(phonebook)
{'Chanti': 9966463846, 'Ramana': 9963666068, 'F T L': 9010023210}
>>> 

Accessing Values in Dictionary :

To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.

>>> data_1={'Id':101, 'Name' : 'V V R Reddy', 'Profession' : 'Web Developer'}
>>> data_2={'Id':102, 'Name' : 'Ramana', 'Profession' : 'Web Designer'}
>>> print ("Id of 1st employer is :",data_1['Id'])
Id of 1st employer is : 101
>>> print ("Id of 2nd employer is :",data_2['Id'])
Id of 2nd employer is : 102
>>> print ("Name of 1st employer :",data_1['Name'])
Name of 1st employer : V V R Reddy
>>> print ("Profession of 2nd employer :",data_1['Profession'])
Profession of 2nd employer : Web Developer
>>> 

Change or Update elements in a Dictionary.

The item i.e., key-value pair can be updated. Updating means new item can be added. The values can be modified.

>>> data_1={'Id':101, 'Name' : 'V V R Reddy', 'Profession' : 'Web Developer'}
>>> data_2={'Id':102, 'Name' : 'Ramana', 'Profession' : 'Web Designer'}
>>> data_1['Profession']='Software Developer'
>>> data_2['Salary']=63000
>>> data_1['Salary']=45000
>>> print (data_1)
{'Id': 101, 'Name': 'V V R Reddy', 'Profession': 'Software Developer', 'Salary': 45000}
>>> print (data_2)
{'Id': 102, 'Name': 'Ramana', 'Profession': 'Web Designer', 'Salary': 63000}
>>> 

How to delete or remove elements from a dictionary?

We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value.

The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. All the items can be removed at once using the clear() method.

We can also use the del keyword to remove individual items or the entire dictionary itself.

>>> # create a dictionary
>>> squares = {1:1, 2:4, 3:9, 4:16, 5:25}
>>> print(squares.pop(4))
16
>>> print(squares)
{1: 1, 2: 4, 3: 9, 5: 25}
>>> # remove an arbitrary item
>>> # Output: (1, 1)
>>> print(squares.popitem())
(5, 25)
>>> print(squares)
{1: 1, 2: 4, 3: 9}
>>> del squares[3]
>>> print(squares)
{1: 1, 2: 4}
>>> squares.clear()
>>> print(squares)
{}
>>> del squares
>>> print(squares)
Traceback (most recent call last):
  File "<pyshell#173>", line 1, in <module>
    print(squares)
NameError: name 'squares' is not defined
>>> 

Functions and Methods

Python Dictionary supports the following Functions :

Functions Description
len(dictionary) Gives number of items in a dictionary.
cmp(dictionary1,dictionary2) Compares the two dictionaries.
str(dictionary) Gives the string representation of a string.

Dictionary Methods:

Methods Description
keys() Return all the keys element of a dictionary.
values() Return all the values element of a dictionary.
items() Return all the items(key-value pair) of a dictionary.
update(dictionary2) It is used to add items of dictionary2 to first dictionary.
clear() It is used to remove all items of a dictionary. It returns an empty dictionary.
fromkeys(sequence,value1)/ fromkeys(sequence) It is used to create a new dictionary from the sequence where sequence elements forms the key and all keys share the values ?value1?. In case value1 is not give, it set the values of keys to be none.
copy() It returns an ordered copy of the data.
has_key(key) It returns a boolean value. True in case if key is present in the dictionary ,else false.
get(key) Returns the value of the given key. If key is not present it returns none.