Python Dictionary construction using a mapping object

Create a Python Dictionary from a mapping object using the dict constructor

Dictionary Updated 10 Sep 2016 1 min read

Overview of dict(mapping)

  • Dictionaries can be created using an existing mapping object.
  • A mapping object is an object that implements the interface as defined by the collections.abc.mapping class.
  • The class dict which implements the dictionary behaviour in Python is derived from the collections.abc.mapping class.
  • In other words, a dictionary can be created from another dictionary. As of now Python has only one mapping type and it is the dict type.

Example

python
# Example Python program that creates

            # a dictionary instance from an existing mapping

            # instance

# A dictionary of pids vs their bytes in physical memory

            pidVsMem = {141:14812,

                        131:29613,

                        129:48349};

# Create a dictionary from an existing dictionary

            snapshot = dict(pidVsMem);

            print("Snapshot:");

            print(snapshot);

# Remove the key-value for the smallest key

            snapshotKeys = snapshot.keys();

            snapshot.pop(min(snapshotKeys));

            print("Snapshot-updated:");

            print(snapshot);

Output

Output
Snapshot:

            {141: 14812, 131: 29613, 129: 48349}

            Snapshot-updated:

            {141: 14812, 131: 29613}