This is part of an ongoing series on learning the Python programming language. If you haven't read them yet, I advise you to catch up on parts one, two, three, four, five, six, seven and eight before reading this article.

Like any other language, Python has support for data structures. A data structure is an object that can hold a set of other objects. Python has 3 basic data structure classes: list, tuple and dict (short for "dictionary").

Lists

Lists are the most basic data structure, they are simply an ordered collection of objects. Objects can be added, moved around and removed at will. Let's look at an example.

veggies = ['carrot', 'broccoli', 'butternut']

In this example we have created a list object called "veggies" which contains 3 strings. The square brackets denote that we're creating a list, and items in the list are separated by commas.

As discussed in the previous blog post, objects can have methods, and lists are no different. Each list object has methods to modify the list.

veggies.append('pea')
print veggies

veggies.insert(2, 'pumpkin')
print veggies

veggies.extend(['turnip', 'radish', 'marrow'])
print veggies

If we run this code, we'll see something like this:

['carrot', 'broccoli', 'butternut', 'pea']
['carrot', 'broccoli', 'pumpkin', 'butternut', 'pea']
['carrot', 'broccoli', 'pumpkin', 'butternut', 'pea', 'turnip', 'radish', 'marrow']

The append method just adds one item to the end of the list. The insert method inserts one item into the list in a specific place. The extend method adds a list of items at the end of the list.

Working With Sequences

You may have noticed that the insert method specified position 2, but when we looked at the output the item had been inserted at the 3rd position. This is because data structures in Python are zero-indexed; in other words, the first position is actually position 0. This may seem weird, but it is due to computers regarding 0 as a fully-fledged number just like 1 or 2, where we generally treat 0 as special. In computers all numbers start at 0.

Lists, tuples (which we'll deal with in the next tutorial) and strings are all sequences; individual items in these structures can be accessed via their position index and they can be spliced (cut up into smaller structures).

To get a particular item in a sequence we use the index syntax as demonstrated below, using the list we constructed earlier:

print veggies[0]
print veggies[2]

This will print the following:

carrot
pumpkin

You can access items in specific positions from the back of the sequence using negative indexes, even if you don't know how long the sequence is:

>>> print veggies[-1]
marrow

There are times when you will want to extract a part of the sequence into another sequence; this is called splicing. You can splice sequences using the same index syntax, but using a colon to separate the starting position from the stopping position.

>>> print veggies[1:4]
['broccoli', 'pumpkin', 'butternut']

You'll notice that the item at position 4 is not included here. This is because that is the position where the splice ends, i.e. don't splice this item.

If you want to splice from a certain point up to the end of a sequence, but you don't know the length of the sequence, you can simply omit the second index, and the splice will include the last item.

>>> print veggies[6:]
['radish', 'marrow']

Similarly, if you want to splice from the beginning of the sequence, you can omit the starting index and the splice will start at the beginning of the sequence. Don't forget the colon, otherwise you'll simply be referring to one item in the sequence.

>>> print veggies[:3]
['carrot', 'broccoli', 'pumpkin']

If you were to omit both the starting and ending indices, so that all you're left with is the colon, you'll get a copy of the entire sequence returned to you.

>>> print veggies[:]
['carrot', 'broccoli', 'pumpkin', 'butternut', 'pea', 'turnip', 'radish', 'marrow']

Lastly, when splicing you can specify the number of items to step over when splicing, by adding another number to the index syntax. Let's take a look at an example.

>>> print veggies[::2]
['carrot', 'pumpkin', 'pea', 'radish']

Using this you can actually reverse a sequence quite easily:

>>> print veggies[::-1]
['marrow', 'radish', 'turnip', 'pea', 'butternut', 'pumpkin', 'broccoli', 'carrot']

I have used a list for the above examples of sequences, but these actions can be used on both tuples and strings.

We'll look at the other two data structures in the next tutorial.


Comments

comments powered by Disqus