sampleqa.in tutorials, python tutorial,python 3 tutorial

Python tutorial


Python Dictionary Object

     Dictionary is a mapped data structure, which has key:value pair as an elements, key is immutable object, values can be mutable

Various Ways to create a Dictionary object in Python

methoddescription
dict()create a empty dictionary object
dict(mapping)create a dictionary from mapping
dict(iterable)create a dictionary from iterable
dict(**kwargs)create a dictionary from key word args

create a empty dictionary object

,
		>>> d={}
		>>>d=dict()
	

create a dictionary from mapping

syntax:
		d=dict(mapping)
	
		>>>def fn(key,value):
		      return key,int(value)
		      
		d=dict(map(fn,'abcdef','123456'))
	

create a dictionary from iterable

		d=dict( [('a',1),('b',2),('c',3),('d',4),('e',5),('f',6)] )	
	

create a dictionary from key word args

		>>>d = dict(one=2,two=3,three=4,four=5,five=6)
	

Adding Elements to Dictionary Object

Remove to Dictionary Object

Update Dictionary element

Iterate Dictionary Object

  • d.keys
  • d.values
  • d.items

ADS