Wednesday, May 13, 2009

Python, using a list variable

In this post I will be discussing the use of lists in Python. In a previous post I already discussed the use of a tuple, where a tuple is fixed a list is dynamic. When you define a tuple the content is set for the rest of the program, when you use a list instead you can change the content.

First start with defining a list variable and placing some data in the list, in this case it will be dutch names. As you can see in the example below we define a list almost the same as a tuple, the difference is the sort of brackets.

As can seen in the example above the variable type in Python can be checked with the type command. In this case we do a type(thelist) and we get a . Now if we want some things out of the list, for example we like to print the entire list we can do a print and the name of the list. However, in most cases you only want a part of the list displayed and not all the content of the list.

The same as we could do with a tuple is what we can do with a list in Python. See the example below:

As you can see you can do a listname[x] where listname is the name of your list variable and x is the number of the element in the list. Now in this case we use positive numbers because we will move in a positive direction. It might be that you do not want this so you can also use negative numbers like in the example below.

As you have been reading the blogpost on using a tuple in Python you might already expected it, you can also get parts of more than one element from a list. this is done via for example a [0:3] extension after the name of the list. This will slice your list and provide you the results.

However, there are more ways on slicing your list, like for example you can use negative numbers or only a endpoint or startpoint for the slice you want from the list.

When you play around with those options you will quickly learn how to get your data out of a list just the way you want it and just the way you need it. That is for the data that is in the list, as already stated you can modify the content of the list. You can for example add data to the list. In python we use for this basically 3 commands. append, insert and extend with all their own characteristics.

append will add a given element to the end of the list so if we do a thelist.append("kees") this will be added at the end of the list. Remember, you can append also add a list variable to a list, this will not result in all the single elements of the list you append to be incorporated into the target list variable. It will add the given list variable to the list. If you want the single elements of a list to be added to a list and become elements of the target list variable you have to use the .extend() function for lists. A mistake quickly made, however only made once after you have been debugging for some time.

insert also will add a new element to the list however with the insert command you can define the location of the new element where append will always add it at the end. for example we can do a thelist.insert(2,"piet") will make sure that the new element piet will be at location 2 in the list. You can easily think of some situations where this can come in handy.

The extend command will add a list to a list, in basic it will concatenate two lists into one which can be very handy in some situations where you build separate lists and finally have to combine the results. Think about parallel processing for example where in the end you will have to combine results to provide a complete overview. As shown in the example you can extend it with a list you build on the fly or a predefined list and just add the list variable as a extension on the list.

Even do it is important that you can remove elements from a list it is also important that you can remove items from a list. For this we have the list.remove command which enables you to remove a element from a list, you can see this in the example below:

The tricky part of removing a element from a list in Python is that if you try to remove a element which is not in the list it will give you a clean message, it will crash. So if you are writing some code it is a good thing to check first if the element is (still) in the list. This can be done by using a "in" command. for example if you want to be sure that element a is in the list you do a "a" in list
where list is the name of your list and a the value to be removed. This will return a boolean value upon which you can decide to execute the remove or not.

1 comment:

Anonymous said...

Thanks for the .insert() feature