Data Structures


Everyone knows what a list is right? We have TODO lists, word lists, homework lists. They are everywhere. Well, they are in computers too. Lists help us organize values we might be interested in into a single collection. We can then perform operations on each element of the list and add or delete items. In Python we can create a list by doing the following:

my_list = [1, 2, 3]

We store lists in variables like we have been doing with integers and strings. The syntax has us encapsulate or surround the values with square brackets.

Try creating your own list with the following values: 1, "2", "three", [4]] and then print out the list!

Some languages enforce that lists only hold values of the same type (e.g. integers only or strings only). Python doesn't care as much. Some people view this positively and some view this negatively. It is definitely nice to have all of the flexibility you want when coding but sometimes it can introduce sneaky bugs that are difficult to track down. Whether you like it or don't like it, you should be careful when doing things like I just had you do.

To get the values out of a list we have created we do the following:


This will give us the value of 2. What!? Shouldn't it be 1? Nope. The answer is 2 because in computers we almost always start at an index of 0. The word I used here is index which refers to the action of finding an object. If you went to a library and wished to get a book, you would look up the book's index which would lead you to where it is on a shelf. The same concept applies to indexes (or indices) in a list. The index is the item's position in the list. We use those square brackets after the variable name to tell Python that we wish to index the list at the desired position.

Python allows us to do some cool things with lists. One of these is the ability to index a list from the back instead of the front. This is called a negative index and when performed on our list would give us the value of 3.


We can also create sublists from existing lists. This is called slicing. It looks just like indexing but we supply two values instead of one.

my_list = ['a', 'b', 'c', 'd', 'e', 'f']
list_1 = my_list[:3]   # ['a', 'b', 'c']
list_2 = my_list[1:5]  # ['b', 'c', 'd', 'e']
list_3 = my_list[3:]   # ['d', 'e', 'f']

We have used the len() function with strings but it can also be used with lists to determine how many objects are in lists.

len(['a', 'b', 'c']) == 3

More Operators and Member Functions

We have seen certain keywords already but certain keywords have different meanings when applied to lists. The in keyword was used previously in a for loop. When used with lists we can have Python tell us if a specific value exists within the list.

'c' not in ['a', 'b', 'd', 'e'] == True

We can also use some of our familiar operators with lists. We can concatenate (combine) two lists together with the + operator.

['a', 'b', 'c'] + ['d', 'e', 'f']  # ['a', 'b', 'c', 'd', 'e', 'f']

We can also combine things together multiple times using the * operator.

['a', 'b', 'c'] * 3   # ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

There might be a time when you want to set multiple values from a list at the same time. Python allows you to do this with multiple assignment.

name, height, age = [ 'reed', 71, 26 ]

This is all really cool but how do you add things to a list when the program is running? Lists have functions that you can call on them. This means that when you have a list like: my_list = [1, 2, 3] you can call functions on my_list. We call these member functions because they are members of List.

my_list = ['a', 'b', 'c']
my_list.append('d') # ['a', 'b', 'c', 'd']
my_list.insert(2, 'z') # ['a', 'b', 'z', 'c', 'd']
my_list.index('b') == 'b' # True
my_list.remove('c') # ['a', 'z', 'c', 'd']
my_list = [4, 2, 3, 5, 1]
my_list.sort() # [1, 2, 3, 4, 5]

my_list = [ 4, 6, 1, 'reed', 'britney' ]
my_list.sort()          # kind of sorts but ultimately doesn't work

# check out ASCII values for why the following list sorts funny
my_list = [ 'Reed', 'britney', 'gary', 'Zeke' ]
my_list.sort()   # [ 'Reed', 'Zeke', 'britney', 'gary']

Try sorting the list we created earlier [1, "2", "three", [4]]!

You should have found an error when trying to sort. That is because Python doesn't know how to sort values of different types. This is the trouble I mentioned you can get into if you create lists with different types. Python gives you some flexibility to do what you want but sometimes that flexibility might make things challenging in the future.

results matching ""

    No results matching ""