Python Primer 2 – Lists and Dictionaries

This is my second post in a series to quickly introduce Python:

Hopefully after the first post we’ve got an idea how to fire up the Python shell, create a variable (a cardboard box) that has some data type – an integer (a number), or a string (a letter or a number, a word, multiple words, multiple lines, whatever you want actually). A quick note about data types – the “type” determines how the data acts. An two integers can be added together like numbers, e.g. 1+1 = 2. But if you have a number inside a string data type, you can’t do mathy stuff. If you do “1”+”1″ you’ll get “11” because they’re strings, and that’s how strings behave.

As it turns out, you can bundle these data types together using things called Lists and Dictionaries. Don’t get discouraged if these make your head hurt at first, it hurt for me too when I first learned about them. And it may be hard to see how or why you might use these things at first, but rest assured they are used extensively.

The List

A list is pretty much what is sounds like. You can get your stuff in the list by referring to its order in the list, thus its called an “ordered” list. Lists start with “[” and end with “]” (called square brackets, or just brackets). List items are separated with commas. Typically you store your list inside a variable, like so:

colors = ["red","purple","blue"]

So if you want to grab just the first item in the list, type “colors”, immediately followed by the list item number enclosed in brackets:

colors[1]
'purple'

Was thinking I would get ‘red’, but actually everything in computers starts with 0. So colors[0] = ‘red’, colors[1] = ‘purple’, and colors[2] = ‘blue’ (Single and double quotes are interchangeable).

Python has a plethora built-in “functions” that I like to think of as mini-programs. You can get a whole list of them and much more at the official Python Documentation. I only mention this because we’re going to use a built-in function to do something fun. Fun if you’re a nerd, like me.

If you wanted a string like “red” to be upper case, but didn’t want to go to the work of redefining it, you could simply use a string function that already exists for it. Python says we need to use a dot “.” after the string we want to make uppercase. Also since it’s a function and “upper” is its name, put some parentheses after it to tell Python that you want to run (execute) it, because that’s how some people a million years ago decided execution should be notated:

"red".upper()
'RED'

But instead of re-writing the string, we’ll just grab it from the list instead since it’s already there:

colors[0].upper()
'RED'

The Dictionary

A dictionary is a way of doing a similar thing to a list, but the items aren’t in any particular order. You grab a particular piece of data (Python calls it a “value”) in the dictionary by referencing its “key”, which is just another piece of data. Usually people refer to dictionaries as being a database of key/value pairs, but that is really only useful if you already know how it works.

I’m sure this has been used in many other tutorials, but I think a good example might be of a set of stock market tickers and their associated share prices. The ticker is the key, the price is the value. A dictionary uses (curly) braces at the start and end, each key/value pair use a colon between them, and pairs are separated by commas. Again, it’s usually stored in a variable:

stocks = {'GOOGL':1189.84, 'AAPL':186.79, 'FB':167.68}

If you wanted to grab Google’s price, do the same dance as with a list, but use the key on the left to get the value on the right:

stocks['GOOGL']
1189.84

If you want to set a new value for ‘GOOGL’ since it’s price has changed, change it like you would a variable:

stocks['GOOGL'] = 1189.92
stocks
{'GOOGL': 1189.92, 'AAPL': 186.79, 'FB': 167.68}

One fun thing you can do using a built-in function is to add a new pair to the dictionary, by “updating” it (lists have a similar “append” function). For the list example I used a function on the string inside the list, here I’m using a function on the dictionary itself, hence “.update()” is attached to “stocks”:

stocks.update({'NFLX':359.97})
stocks
{'GOOGL': 1189.92, 'AAPL': 186.79, 'FB': 167.68, 'NFLX': 359.97}

I hope that’s useful. Next up is loops and conditions.