programplug Logo




Python Dictionary


Dictionary work as a container that hold data.Dictionary is a set of key and value pair,enclosed within curly braces.Dictionary is mutable i.e., value can be updated.

item ={1,2,'Rahul','Rohit',3,4,'a'}  
print item

output:{1,2,'Rahul','Rohit',3,4,'a'}


Accessing Dictionary key and Values

item = {key1:value1,key2:value2}
item1 ={'name':'Rahul','id':123}
print item1

output:{'name':'Rahul','id':123}


Updating Dictionary Elements

item1={'rollno':1, 'Name':'Rahul', 'Class':'12'}  
item2={'rollno':2, 'Name':'Mohit', 'Class':'11'}  
item1['Class']='10'  
item2['Grade']=1  
item1['Grade']=2  
print item1  
print item2

output:
{'rollno':1, 'Name':'Rahul', 'Class':'10', 'Grade':1}
{'rollno':2, 'Name':'Mohit', 'Class':'11', 'Grade':2}


Deleting Dictionary Elements

del statement is used for deleting operation.

item={1:'Rahul', 2:'Mohit', 3:'Kumar'}  
del item[3]  
print item    
del item  
print item 

output:
{1:'Rahul', 2:'Mohit'}
NameError: name 'item' is not defined


Built-in Functions

Dict.cmp()Compares elements of both dict
Dict.len()Total length of the dictionary
Dict.str()Printable string representation of a dictionary
Dict.type()Returns the type of the variable
Dict.clear()Removes all elements of dictionary
Dict.copy()Returns a shallow copy of dictionary
Dict.fromkeys()Create a new dictionary with keys from seq and values set to value
Dict.get()For key, returns value or default if key not in dictionary
Dict.has_key()Returns true if key in dictionary dict, false otherwise
Dict.items()Returns a list of dict tuple pairs
Dict.keys()Returns list of dictionary keys
Dict.setdefault()Set key as default if key is not already in dict
Dict.update()Add dictionary
Dict.values()Returns list of dictionary dict’s values





Our Services

Developing Cheapest Websites
Offer Digital Marketing
Provide College Projects
Attractive Website Developing
Summer Training,Industrial Training
SCO,SMO

Contact Us

Dehradun Uttarakhand India
www.programplug.com
info@programplug.com
0000000000

Other Link

About Us

Contact Us

Disclaimer

Privacy Policy