Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
• Live Instructor-led Classes
• Expert Education
• 24*7 Support
• Flexible Schedule

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

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
Previous Next

Learn SQL in 16 hrs from experts

### "0 Responses on Python Dictionary"

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

## Sales Offer

• To avail this offer, enroll before 17th December 2018.
• This offer cannot be combined with any other offer.
• This offer is valid on selected courses only.
• Cashback will be accumulated in Intellipaat e-wallet
• Please use coupon codes mentioned below to avail the offer