Hey Everyone!

So today I want to talk about the different ways we store data in python. Now when I say this, I don’t mean it the same way I did when we talked about variables and other data structures. You can check that out here if you haven’t seen that yet. Today, we’re talking about just storing data, like variables or raw data like integers or booleans.

The way we do this is to basically list out the data in a couple of different ways. The most common ways to do this are lists (or arrays), tuples, and Dictionaries.

Lists

A list in python is exactly what you think it is. I simply list out the data you put in them and allows you to refer to that data by indexing them. We create lists by putting data between a pair of square brackets. Let me show you an example:

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’]

As you can see our list contains three strings, ‘Item 1’, ‘Item 2’, and ‘Item 2’. However lists do not care about the data types of the things you put in them. This means we can add more items to our list, even if they aren’t strings.

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’ , 4 , 5 , 6]

The last three pieces of data in our list are just integers. We can also add boolean values, and even other variables into our list. But this doesn’t really mean anything if we can’t access the data we put in our list, so let me show you how to do that.

How to get into Lists

Every item in a list has an index. This is a number that refers to a specific item in the list. The indexes start at 0 and increase by one until you get to the end of the list. Because of this it is important to remember that the nth item in your list will have an index of [n-1]. You can access the information in a list by typing the name of the list followed by the index of that item wrapped in square brackets.

Ok thats really confusing so let me just show you:

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’ , 4 , 5 , 6]
  2. y = x[0]

In this example, the variable y, would have the data stored in the first spot in the list, which is ‘Item 1’. You don’t have to assign the data to a variable to reference it. For example if I wanted to print the string ‘Item 3’, I could do the following:

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’ , 4 , 5 , 6]
  2. print(x[2])

Notice I’ve used the index [2] here to refer to the third object in our list. It is very important to make sure you are actually putting in the right index so that your programs run correctly.

We can also change specific data in our list with these indexes. If I wanted to change the the integer 5 in the list to the string ‘Item 5’, I could do it like this:

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’ , 4 , 5 , 6]
  2. x[4] = ‘Item 5’

I use the index [4] here because the integer 5 was the fifth item in the list before I changed it. the list now looks like this.

  1. x = [‘Item 1’ , ‘Item 2’ , ‘Item 3’ , 4 , ‘Item 5’ , 6]

Tuples

Tuples are a lot like list. I mean a lot. They are almost identical in every way. To create a tuple we use round parenthesis instead of square brackets, like this:

  1. (‘This is a tuple’)
  2. [‘This is a list’]

The biggest difference between a tuple and a list is that you cannot change the data stored inside a tuple with rewriting the entire thing. Other than that you can do everything that you can do to a list to a tuple.

  1. x = (‘Item 1’ , ‘Item 2’ , ‘Item 3’)
  2. y = x[0]
  3. print(y)

The above bit of code stores the first item within the tuple to the variable y, then prints it. Notice how when referring to things with a tuple, we still put square brackets around the index.

Dictionaries

Dictionaries contain the most detailed data in python. To create a Dictionary, we use a pair of curly brackets (these are above the square brackets on your keyboard). These not only store data in the order you enter them, but also give them keys with which they can be easily indexed. Here is an example of one:

  1. x = {‘Item 1’ : ‘Triangle’ , ‘Item 2’ : ‘Square’ , ‘Item 3’ : ‘Circle’}

So when we want to refer to something in a dictionary, we don’t use a numerical index. Instead we refer to them by the key that they are given. For example, if I wanted to pull the string ‘Circle’ out of the dictionary:

  1. x = {‘Item 1’ : ‘Triangle’ , ‘Item 2’ : ‘Square’ , ‘Item 3’ : ‘Circle’}
  2. x[‘Item 3’]

Again, we still put square brackets around the index. I won’t go into crazy detail about the python dictionary write now. To be honest, I rarely ever use them. But its important to know all of the options you have for storing data so I wanted to include them in the post.

Leave a Reply