Session – 14 (Dictionary)

  • Dictionary
  • Dictionary Operations
  • Dictionary Functions
  • Dictionary Methods

1.Dictionary

Creating a dictionary is as simple as placing items inside curly braces {} separated by commas.An item has a key and a corresponding value that is expressed as a pair (key: value).While the values can be of any data type and can repeat, keys must be of immutable type (stringnumber or tuple with immutable elements) and must be unique.

  • Keys
  • Unique – No dup..
  • Immutable data type
  • Strings
  • Numbers
  • Tuples
  • One entry per key
  • For multiple assignments last assignment wins
  • On multiple occurrence of Key Python removes old Key by default
  • Values
  • May be unique
  • Any type
  • {.:.,.:.,.:.}
  • {‘Key’:’Value’}

2.Dictionary Operations

  • D = {.,Key_x:Value_y,.}
  • D[K] : ← V(K)
  • D[K] = V : D ← D[K:V]
  • del D[K] : D ← rmv_D[K:V]
  • D.clear() : D ← clr_D
  • del D : ← del_D

3.Dictionary Functions

  • len(D) : D_len

4.Dictionary Methods

  • D.copy()
  • D.fromkeys(K, /v)
  • D.get(k, /s)
  • D.items()
  • D.keys()
  • D.values()
  • D.setdefault(kx, /s)
  • D.update(F)

Leave a comment