Dictionary in Python



Dictionary is used to represent a collection of keys and values pair of data.



Syntax
fruitsCollection= {'apple', 'pear', 'orange', 'banana'}
print fruitsCollection

output
set(['orange', 'pear', 'banana', 'apple'])
userCollection = {'1': 'Mac', '2': 'ALEN','3':'Kenneth'}

del userCollection['1']

print userCollection

output

{'3': 'Kenneth', '2': 'ALEN'}
for i, j in userCollection.items():
          print(i,j)

Output
('3', 'Kenneth')
('2', 'ALEN')


Dictionary Manipulation functions

dict1 = { }                               Empty dictionary
dict2 = {'Aa': 2, 'Bb': 3}           Two-item dictionary
dict2['session']                         Indexing by key
dict2.has_key('UserName'),      UserName' in dict2 membership test
dict2.keys( ), dict2.values( ),
dict2.items( )                           lists of keys, values, items
dict2.copy( ),
dict2.update(dict1)                   shallow copy, dict merging
dict2.get(key, default=None)   "indexing"
len(dict1)                                 Length (number stored entries)
dict2[key] = 300                      Adding/changing
del dict2[key]                          deleting
dict = dict(zip(klist, vlist))        Construction

Dictionary in Python Reviewed by Rupesh on 10:15 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.