Python training : Dictionary




Dictionary

Dictionary is a datatype where “keys” are used to index each value. This can be considered like a list where each value gets a “descriptive index” instead of a number as index. Other languages uses terms like “associative arrays” or “associative memories” to represent similar datatype.


Different ways of creating dictionary:


>>> a = {'joe' : 85 , 'peter' : 88 , 'jack' : 90}

>>> b = dict (joe = 90 , peter = 85 , jack = 88)

>>> c = dict ( [  ('joe' , 90)  ,  ('peter' , 85) ,  ('jack' , 95)  ]  ) # here argument is a list of tuples

>>> print (a)
{'joe': 85, 'peter': 88, 'jack': 90}

>>> type (a)
<class 'dict'>

>>> len (a)
3




Adding new key and value:


>>>  a['kris'] = 85

>>> print (a)
{'joe': 85, 'peter': 88, 'jack': 90, 'kris': 85}

>>> del a['kris']

>>> print (a)
{'joe': 85, 'peter': 88, 'jack': 90}

>>> a = str(a)

>>> print (a)
{'joe': 85, 'peter': 88, 'jack': 90}

>>> print (a[0:5])
{'joe




Checking whether a key is present:


>>> 'peter' in a
True

>>> 'michel' not in a
True




Dictionary methods


>>> a = {'joe' : 85 , 'peter' : 88 , 'jack' : 90}

>>> b = a.get('peter',0)

>>> print (b)
88

#get() method will return the value of the given key if that key is present, else it will return the second argument. 

>>> c = a.get('kris',0)

>>> print (c)
0 

>>> b = a.keys()

>>> print (b)
dict_keys(['joe', 'peter', 'jack'])

#Note: keys() method return object with type “dict_keys”, to convert this to a list , you can use list()

>>> b = a.values()

>>> print(b)
dict_values([85, 88, 90])

>>> print (list(b))
[85, 88, 90]




Creating two dimensional dictionary: Method 1: Use list as values


>>> marks = {'peter': [80,85,90], 'jose' : [85,90,95]}

>>> print(marks['jose'])
[85, 90, 95]

>>> print (marks['jose'][0])
85

>>> marks['jose'][0] = 86

>>> print (marks)
{'jose': [86, 90, 95], 'peter': [80, 85, 90]}

#Note: in this method ,If we consider ‘jose’ and ‘peter’ as rows in the two dimensional dictionary, columns can be represented only using numbers from 0 to ‘n’ (index of list)




Method 2 : use dictionary as values


>>> marks2= {'jose' : {'maths': 85 , 'english': 90, 'science' : 95} , 'peter' : {'marks': 80 , 'english':85 , 'science': 90}}

>>> print (marks2['jose']['science'])
95

>>> marks2['jose']['science'] = 96

>>> print (marks2)
{'jose': {'science': 96, 'maths': 85, 'english': 90}, 'peter': {'science': 90, 'marks': 80, 'english': 85}}




Method3 : Using “defaultdict” class


>>> from collections import defaultdict

>>> marks3 = defaultdict(dict)

>>> marks3['jose']['maths'] = 85

>>> marks3['jose']['science'] = 95

>>> marks3['peter']['maths'] = 80

>>> marks3['peter']['english'] = 85

>>> print(marks3)
defaultdict(<class 'dict'>, {'jose': {'science': 95, 'maths': 85}, 'peter': {'maths': 80, 'english': 85}})

>>> print(marks3['jose']['science'])
95




Python Exercises

Create a database in the following format

Values	=	Router1		1.1.1.1	 	zframez		zframez
Keys	=	(name)		(IP)		(username)	(pwd)

1. Write a python program to print the value of a given key

2. Write a python program to check whether the given key is present, if present print the value , else add a new key and value

Create a database in the following format

	Interface		IP			status
	
1	Ethernet0		1.1.1.1			up
2	Ethernet1		2.2.2.2			down
3	Serial0			3.3.3.3			up
4	Serial1			4.4.4.4			up

3. Write a python program to find status of a given interface

4. Write a python program to find interface and IP of all interfaces which are up

5. Write a python program to count how many ethernet interfaces are there

6. Write a python program to add a new entry to above database

Next chapter: Python training : Functions, Lambda functions, Local and Global variables

Previous chapter: Python training : Lists, Slicing examples, List methods and Tuples