Python Dictionary

Python has in-built dictionary called dict, which we can use to create an arbitrary definition of the character string. The dict() is a constructor which is used to create instances of the class dict. Dictionary is a collection, whose values are accessible by key. The dictionary is mapping of key & value. The order of elements in a dictionary is undefined. We can iterate over keys, values and key-value pairs.

The value can be any type and key occurs only once in the dictionary. The general syntax is -

{key1: value1, key2: value2, key3: value3, ....}

To create dictionary, first create an empty dictionary using dict() function, like this -

flower = dict();

Then define the definition work by using dictionary words followed by square bracket.

>>> flower = dict();
>>> flower['yellow'] = 'Marigold'
>>> flower['red'] = 'Rose'
>>> flower['white'] = 'Lilly'
>>> print(flower)
{'white': 'Lilly', 'red': 'Rose', 'yellow': 'Marigold'}
Output of the above code
Python Dictionary

As the dictionary defines a key value pair.
We can also define dictionary as -

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> print(dict)
{'White': 'Lilly', 'Red': 'Rose', 'Yellow': 'Marigold'}

In dictionary, the key is also defined as unique

Access Dictionary

Use dictionary index to access value, like -

>>> dict ={'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> print dict['Yellow']
Marigold
>>> print dict['Red']
Rose

Update Dictionary

Updating dictionary data is very simple. For this, we only have to reassign the index value, like -

>>> dict ={'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> dict['Yellow'] = 'Tulipa'
>>> print(dict)
{'White': 'Lilly', 'Red': 'Rose', 'Yellow': 'Tulipa'}




Empty Dictionary Element

To clear all elements of a dictionary, use clear() method, like -

>>> dict.clear()
>>> print(dict)
{}

Copy a dictionary

The copy() method is used to return the copy of a dictionary.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> dict1 = dict.copy()
>>> print str(dict1)
{'White': 'Lilly', 'Yellow': 'Marigold', 'Red': 'Rose'}

Popped Element from a Dictionary

The pop() method is used to return and remove an element from a dictionary having the given key.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> ele = dict.pop('Red')
>>> print 'The removed element is ',ele
The removed element is  Rose
>>> print 'The dictionary is -', dict
The dictionary is - {'White': 'Lilly', 'Yellow': 'Marigold'}

Create a new dictionary from given sequence

Python has predefined fromkeys() method to create a new dictionary from the given sequence of elements.
Suppose the keys and values sequence provided by the user are -

keys = {'one', 'two', 'four', 'three'}
values = {'cat', 'dog', 'fox', 'elephant'}

The following code creates a dictionary from the given sequences -

>>> keys = {'one', 'two', 'four', 'three'}
>>> values = {'cat', 'dog', 'fox', 'elephant'}
>>> items = dict.fromkeys(keys, values)
>>> print items
{'four': set(['elephant', 'fox', 'dog', 'cat']), 
'one': set(['elephant', 'fox', 'dog', 'cat']), 
'three': set(['elephant', 'fox', 'dog', 'cat']), 
'two': set(['elephant', 'fox', 'dog', 'cat'])}

Check existence of a key in dictionary

To check the existence of a key in a dictionary, use the in operator or the has_key() method.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> k = 'Red'
>>> k in dict
True
>>> dict.has_key(k)
True

Iterate over dictionary items

We can easily iterate over the dictionary keys, values and items.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
for key in dict.keys():
	print key
for value in dict.values():
	print value	
for item in dict.items():
	print item	




Check key existence

With the help of in operator, we can check the existence of a key in a dictionary. It returns boolean value either true or false.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> "Red" in dict
True
>>> "Black" in dict
False

Get key list

Python provides keys() method to get list of all the keys in a dictionary.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> dict.keys()
['White', 'Red', 'Yellow']

Get value list

Python provides values() method to get list of all the values in a dictionary.

>>> dict = {'Yellow': 'Marigold', 'Red': 'Rose', 'White': 'Lilly'}
>>> dict.values()
['Lilly', 'Rose', 'Marigold']