Python 3-Dictionary

Python Dictionary

Dictionary data type is a also a collection of elements which uses a value key pair. To access any values from Dictionary, we need to use the specific key pair of it.

Representation of Dictionary ->  {}

Dictionary Creation:

To create a Dictionary, we can use the empty braces, or we should call the direct function call

>>> d = {} #Empty braces represents the creation of empty Dictionary 
>>> print (d)
{}
>>> a = dict() #The function call represents the creation of empty Dictionary
>>> print(a)
{}
>>>

To create a Static Dictionary, refer the following syntax:

Syntax -> {key:value, key1:value1, key2:value2}

Also using dict function, we can pass an iterator with two index elements which acts as a key value pair for dict object creation

Syntax -> dict([(k,v), ‘kv’, [‘k’,’v’]])

dict(key=value, key1=value1, key2=value2,…)

Examples:

>>> d = {'key':'value','k':'v','k1':'v1'}
>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1'}
>>> x = dict(['kv',('k1','v1'),['a','apple']])
>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>>

All keys in the dictionary should be immutable data type. If we pass an mutable object as a key value, then the interpreter will raise an error.

Adding elements to a Dictionary:

To add the elements into a dictionary we can use the following syntax:

Syntax -> obj[key] = value

Examples:

>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1'}
>>> d['a']='apple'
>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> d['b']='banana'
>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>>

We can access the Dictionary values through the respective keys to access the particular values.

Examples:

>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>> d['k']
'v'
>>> d['a']
'apple'
>>>

Modification of values in Dictionary:

To modify the values in dictionary, we can simply access the key value and assign the new values to the key. Instead of creating a duplicate record, it will get updated to the existing record of dictionary.

Examples:

>>> d
{'key': 'value', 'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>> d['k']='new value'
>>> d
{'key': 'value', 'k': 'new value', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>>

Deleting an entry from Dictionary:

To remove the entry from dictionary we can simply use the del keyword or built in methods of python dict objects.

Examples:

>>> d
{'key': 'value', 'k': 'new value', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>> del d['k']
>>> d
{'key': 'value', 'k1': 'v1', 'a': 'apple', 'b': 'banana'}
>>> d.pop('b')
'banana'
>>> d
{'key': 'value', 'k1': 'v1', 'a': 'apple'}
>>> d.popitem()   #deletes last index element
('a', 'apple')
>>> d
{'key': 'value', 'k1': 'v1'}
>>>

The dictionary object method clear is used to clear all the elements in the dictionary

>>> d
{'key': 'value', 'k1': 'v1'}
>>> d.clear()
>>> d
{}
>>>

Dictionary copy:

Copy() -> used to copy the exact dictionary in another memory location

>>> x = dict(['kv',('k1','v1'),['a','apple']])
>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> id(x)
24505968
>>> y=x.copy()
>>> y
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> id(y)
24505872
>>>

Dictionary get:

Get() -> used to fetch the value for a key of the dictionary object. If key is not available in the dict or pass the default value given in the function call.

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> x.get('k')
'v'
>>> x.get('b') #does nothing as there is not key ‘b’ in dict x
>>>
>>> x.get('b','default')
'default'
>>> x.get('a','default')
'apple'
>>>

Dictionary items:

Items() -> returns all the key value pairs of a dictionary object as a iterator

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> x.items()
dict_items([('k', 'v'), ('k1', 'v1'), ('a', 'apple')])
>>>

Dictionary keys:

Keys() -> returns all the keys of a dictionary object as a iterator

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> x.keys()
dict_keys(['k', 'k1', 'a'])
>>>

Dictionary setdefault:

Setdefault() -> used to fetch the value of the key specified, if the key is not available, it will create the new key with the value None, if value is not specified else the key will be created with the value created.

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple'}
>>> x.setdefault('k')
'v'
>>> x.setdefault('b')
>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': None}
>>> x.setdefault('c','cranberry')
'cranberry'
>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': None, 'c': 'cranberry'}
>>>

Dictionary update:

Update() -> used to update the dictionary record, ie, merging two dictionaries into one.

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': None, 'c': 'cranberry'}
>>> y
{'a': 'antman', 'b': 'batman'}
>>> y.update(x)
>>> y
{'a': 'apple', 'b': None, 'k': 'v', 'k1': 'v1', 'c': 'cranberry'}
>>>

In the above example, the dict y is updated with the new keys from x and also for the same keys, the values of x is updated in y.

Dictionary values:

Values() -> used to fetch all the values of existing dictionary

Examples:

>>> x
{'k': 'v', 'k1': 'v1', 'a': 'apple', 'b': None, 'c': 'cranberry'}
>>> x.values()
dict_values(['v', 'v1', 'apple', None, 'cranberry'])
>>>