Date Modified Tags Python

Warning! This article is very much under-construction! The title and everything else is definitely subject to change. I will be editing this article and releasing it live, as I write it.

Lesson 3 - Lists

So, we have learned about the primitive datatypes in Python and now its time to learn about another, more complex data type, the list. Lists are the most prevalent datatype in Python and you will encounter them all the time!

So, what is a list?

Just like it sounds it is a list of some other data type. They are very closely related to the idea of arrays in other programming languages.

Creating a list

Let's stop messing around and create a list in Python. Open up your interactive shell by typing python and type the following:

>>> my_list = [1, 2, 3, 4, 5]
>>> my_list
[1, 2, 3, 4, 5]

Notice that a list in Python is surrounded by brackets ([]) and that each element is seperated by a comma. We then assign the list we have created to the variable named my_list.

I want you to understand the idea that in Python, a variable can be of any type so go ahead and try the following:

>>> my_list = [1, 2, 3, 4, 5]
>>> my_list = 100
>>> my_list

It is eventually important to understand that in Python everything is an object. You can think of Python as having objects and then names. An object can be of the types you have seen so far, lists, intergers, floats, and strings (as well as others) and each object is assigned a name.

When we say that my_list = 100 we are really just taking the name my_list and assigning it to the new integer object 100.

If that doesn't make sense right now, then just ignore it and file it away for later. Let's get back to lists.

Accessing list elements

You access individual list elements by using something called array notation. In most sane programming languages, you access the individual elements like the following:

>>> my_list = [1.201, 99.65, 13.43]
>>> my_list[0]
>>> my_list[1]
>>> my_list[2]

Notice above that this is a list of floats. In Python, you can have lists of almost any data type.

Lists are 0 indexed

Notice above that the first element of the list is accessed not as the 1st element but as the 0th element. This is called 0 indexing and it is a common practice in programming.

Using a function to get the length of a list

We can use someting called a function to get the length of a list in python.

If you remember functions from math, they take certain inputs and produce an output. Functions in programming follow this same idea.

Let's use the len() function to get the length (number of elements) of a list:

>>> my_list = ['Hello', 'Python', 'World!']
>>> len(my_list)

As you can see, Python returns for us the length of the list. In this case my_list is made up of 3 string elements.

We can turn a string into a list

I want you to think for a moment about what a string actually is.

It is a list of characters, right? So, it should come as no surprise that we can easily turn a string into a list.

>>> my_string = "Hello"
>>> list(my_string)
['H', 'e', 'l', 'l', 'o']

We can also access the individual elements of a string directly by using array notation.

>>> my_string = "Hello"
>>> my_string[0]
>>> my_string[1]
>>> my_string[2]

So that is cool huh? Let's see if we can just change the elements of a string:

Strings are immutable, lists are not

>>> my_string[0] = 'A'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Ok, so now you have come across your first error and it is a type error. In Python, strings are immutable which means they cannot be changed.

What we can do though is change a string into a list, change the elements we want, and then change is back into a list.

>>> my_string = "Hello" 
>>> my_string = list(my_string) # we turn my_string into a list
>>> my_string
['H', 'e', 'l', 'l', 'o']
>>> my_string[0] = "A" # assign the 0th element of my_string to be the character A
>>> my_string
['A', 'e', 'l', 'l', 'o']
>>> my_string = ''.join(my_string) # use the string method/function join to turn my_string back into a string
>>> my_string


If you looked closely above we use a strange function with ''. in front of it. This is actually called a method. A method is just a function that belongs to an object. What is the ''? It is just an empty string object.

So, we create a string, and then called its .join() method in order to turn our list back into a function.

List methods

Let's take a look at some helpful methods that lists have.

(Coming soon)

I am still working on this series of articles but they have been delayed because I am finishing up the Analysis of Algorithms class I have been taking this term.

As usual, feel free to comment below or contact me if you come across any errors in this post!


comments powered by Disqus