Home Python Dictionaries
Post
Cancel

Python Dictionaries

When you start working with python code, you will quickly come across “dictionaries”. If you have worked in other programming languages, you may know them as arrays.

Flat arrays

Arrays store elements into a single dictionary. Here are examples of a flat array:

1
2
3
myDictionary = [1,2,3,4]
myDictionary = ["one","two","three","four"]
myDictionary = [1.0,2.4,3.6,4.6]

You would access each element via a index number, starting with 0.

1
2
print(myDictionary[1])
print(myDictionary[3])

Multi-dimensional arrays

Multi-dimensional (named) arrays use a key-value approach.

1
2
myDictionary = {"1":"One","2":"Two","3":"Three","4":"Four"}
myDictionary = {"Fred":"Apples", "Joan":"Pears", "Sam":"Bananas"}

Notice the change from Brackets (“[]”) to curly braces (“{}”).

More to come…

This post is licensed under CC BY 4.0 by the author.