Python Dictionary

Dictionary in Python

What is dictionary?

Python dictionaries are collection of data in the form of key:value pair. It is always enclosed with curly-bracket.



Dictionary Properties

Following are the properties of python dictionaries :

  • Ordered : Dictionary items have a fixed order that cannot be changed.
  • Changeable : Dictionary items are changeable means items can be added or removed from the dictionary.

  • Not Allow Duplicate : Dictionary items can not be used multiple times in the same dictionary.

  • Indexed : Dictionary items can be sliced or indexed with the help of key and it returns the assigned value of the given key.


Dictionary Methods

Python dictionary methods help to manipulate the dictionaries. Following are built-in methods :

  •  dictionary.copy()  : Used to copy a dictionary.
  •  dictionary.items()  : Used to list all the elements of the dictionary.
  •  dictionary.keys()  : Used to list all the keys of a dictionary.
  •  dictionary.values()  : Used to list all the values of a dictionary.

  •  dictionary.get()  : Used to find the value of specific key.

  •  dictionary.setdefault()  : Used to find the value of specific key.

  •  dictionary.fromkeys()  : Used to create a dictionary with a specific key-value pair.

  •  dictionary.update()  : Used to update a dictionary by adding key-value pair to it.

  •  dictionary.pop()  : Used to remove a element by using key of that element.
  •  dictionary.popitem()  : Used to remove the last element of a dictionary.

  •  dictionary.clear()  : Used to remove all the elements from the dictionary.



Try to assign your name, class, roll and school in the python's dictionary.

Comments