bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Python Dictionary

 

Dictionaries are unordered collections of keys and value pair. Dictionaries are mutable. Key should be unique for each value. Key must be unique and immutable. Key and value is separated by a : (colon) . This pair is known as item and items are by comma. In terms of their main properties, dictionaries are:

  • Accessed by key, not offset
  • Unordered collections of arbitrary objects
  • Of the category mutable mapping
  • Tables of object references (hash tables)
  • Variable length, heterogeneous, arbitrarily nestable

e.g.

D1={10:"abc",20:"xyz"}
print D1

Output

{10:’abc’,20:’xyz’}

 

Accessing Values

Dictionaries value is accessed by their keys.

e.g.

D1={10:"abc",20:"xyz"}

print D1[10]

Output

abc

 

  • Updation

In dictionary you can modify the existing value and also you can add new item.

e.g.

D1={10:"abc",20:"xyz"}

D1[20] = "ijk"

D1[30] = "cde"

print D1

Output

{10:’abc’,20:’ijk’,30:’cde’}

 

Deletion

Deletion is performed by using key and del is used to delete the item. In this you can delete individual item and also complete dictionary.

 e.g. Deletion individual item

D1={10:"abc",20:"xyz"}

del D1[20]

print D1

Output

{10:’abc’}

 

e.g. Deletion complete dictionary

D1={10:"abc",20:"xyz"}

del D1

print D1

Output

Traceback (most recent call last):

File “C:/Python27/dict.py”, line 3, in

print D1NameError: name ‘D1’ is not defined

 

Learn more about PYTHON TOOLKIT in this insightful blog now!

 

Functions and Methods 

Functions Description
len(dictionary) Returns number of items in a dictionary
cmp(dictionary1,dictionary2) Compares the two dictionaries
str(dictionary) Returns the string representation of a string
dictionary.values() Return the all values of dictionary
dictionary.keys() Return the all keys of dictionary
dictionary.clear() Remove all items of a dictionary and returns an empty dictionary
dictionary.items() Returns the all  items of dictionary
dictionary.copy()  Returns an ordered copy of the data

"0 Responses on Python Dictionary"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 07th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Python Dictionary.