List Methods

To get offline help with list methods you can input in a Python Interpreter:

list.append()

The append() method will add an item to the end of the list. It will push onto the stack.

list.extend()

In the same way append() adds one item, extend() will add numerous items.

list.insert()

the insert() method will insert an index at a specific index, where the first argument is the index number and the second argument is what to insert there. Here we insert the string 'index1' at lister[1].

list.index()

The index() method will take the argument of something in the list and give you back the index number.

Notice in the next example index() returns the first occurance of the index.

If you are trying to get every occurance of an index (in our example, every 't'). We can use the built-in function enumerate() here with a for loop. The i is the index number and the val is the value of the index in lister. It will act like any other for loop, but will have two values for each iteration, the i (number starting at 0 stepping up with each iteration) and the val (the value of each index in the list).

list.count()

The method count() gives you the number of occurances of it's argument.

list.sort()

The method sort() will sort the indexes in order.

You will notice that it changes the list in place. You lose the original list. This may or may not be your intentions, but if it's not, the built-in sorted() function will create a new list and save the old.

list.reverse()

The method reverse() will reverse the order of the list.

Again you will notice that the method reverse() like sort() changes the list in place. IF you would like to make a new list and save the old, use the built-in function reversed()

list.pop()

The method pop() will remove an index from the list. With no arguments, just pop(), it will remove the very last index from the list in place AND it will return the index that was removed.

Here we use list.pop() to remove the last index. We also that it returned the index 'r'. So if you need the index that you popped off the stack you could then save it to a variable, and if not, you could just execute list.pop() which will still pop the last index off the stack.

Here we give the argument 0 to pop(). This will instead of popping off the last index of the list, will pop the index of the specified number. We gave the argument 0, so the the first index of the list was popped and also returned, indicated by the value shown 't'.

list.remove()

The method remove() will remove the first occurance of the given argument.

The first example we see the first occurance of 't' was removed, but not the second. IN the second example we recreated the list, and removed 'e'. Again only the first occurance was removed.