Link

Jump Start with Python: Dictionary

Dictionary is an abstract data type, composed of a collection of key value pairs enclosed within the braces, where a key is immutable and must be unique. A dictionary is an unordered collection and is mutable in nature. It can contain the mixed types, as the keys and their corresponding values.

Python accounts for the dictionary under mapping types. Dictionaries are also called the hash tables or associative arrays. Below is a simple example of Python dictionary:

Dictionary Example
Dictionary Example

Creating a Dictionary

A dictionary in Python can be created in two different ways:

  1. Just enclose (key, value) pairs between the braces.
    Syntax: dict_name={ “key1”: “val”, “key2”: “val”, “key3”: “val”, —, “keyn”: “val” }
  2. Using dict constructor.
    Syntax: dict_name=dict(key1=“val”, key2=“val”, key3=“val”, —, keyn=“val”)

Some Facts About Dictionary

  • A dictionary without any positional argument is called an empty dictionary.
  • A dictionary is an unordered set of the keys, value pairs and are indexed, using the keys specified in it.
  • The keys in a dictionary are unique and if multiple assignments to a key are attempted, the latter value will be assigned to the respective key discarding the value, previously assigned to it.

Basic Operations

  • Accessing a Dictionary
  • Updating a Dictionary
  • Deleting Dictionary Elements
  • Deleting a Dictionary

Dictionary Methods & Functions

  • Length
  • String
  • Clear Method
  • Pop Method
  • Pop Item Method
  • Copy Method
  • From Keys Method
  • Get Method
  • Items Method
  • Keys Method
  • Values Method
  • Update Method

My article “Jump Start With Python: Dictionary – Part Eight” is the eighth article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start With Python – Part 7 (Tuples)“, Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *