Friday, June 1, 2018

How do you construct a Python dictionary?

Python dictionary has features common to JSON values.


Python dictionary has keys and associated with keys there are lists.
{key:[values],key[values]..}

Here is an example:

graph={"A":["C","B"],
       "B":["A","C"],
       "C":["A","B","D","E"],
       "D":["C","E"],
       "E":["C","D"]
      }


Defining a dictionary in Python.


Dict_0


In Python you can do the following to access keys and associated lists.

You can find the keys as show here:


Dict_1

You can gather all of the items in dictionary using the following:

Dict_2

You can list out the values only as shown here:


Dict_3

You can also access the lists by looping as in the following:


Dict_4


More in my next...

No comments: