Python Programming pt.5: Introduction to Lists
If you have an amount of data you want to group together, how would you do it? One method might be a series of variable assignments. A shopping list might look like this:

This has some problems though, mainly that you have to work with each variable manually in the code. If you want to print all items on your shopping list, then you must use a print statement for each item. To print a certain item, you need to code a print statement to print that specific item. This approach may work for a small shopping list but becomes impractical for longer ones. Most programming languages have data types called “arrays” and “lists” to help you work with these collections of data, and in this guide, I’m going to teach you how to work with lists in python.
The first thing you need to know is how to define a list in python. Lists are assigned like any other variables, but the values are put between square brackets with each item separated by a comma.
The same shopping list can be implemented like this:

Not only have the number of lines of code been reduced, but we can now work programmatically with our list. The most fundamental thing about lists is that every item has an ‘index’, which describes its location in the list. This location starts counting from 0, so the first item is index 0, with the second item being index 1, etc. You can access an item in the array with its index number.

You can also overwrite an item of a list by assigning that index of the list to a new value.

There are many tricks you can do with list indexes, which can be found here.
This is all well and good for modifying existing items, but what about adding more items? The main way this is done is with the append method, which adds an item to the end of the list.

There are two methods for removing items from a python list. The pop method removes an item from a certain index, while the remove method removes an item that matches the value it is given.

I hope you have been able to follow along with this tutorial. I have written code snippets showing each of these features which is available to download from this github repo. Feel free to play about with it or even write your own code to see how lists work.
This post was just an introduction. In my next post I will show you some practical examples of working with lists.