Go to: Na-Rae Han's home page  

Python 2.7 Tutorial

With Videos by mybringback.com

#10: List Methods

                          << Previous Tutorial           Next Tutorial >>
On this page: list methods, .append(), .insert(), .remove(), .pop(), .extend().

Get Started

Video Summary

  • The script in this tutorial contains only one line of code: a list called myGoals. The interactive shell session follows the execution of the script with a few command designed to manipulate the list.
  • In this lesson, we learned about a few more functions pertaining to lists within Python. The .append() method allows you to add a further item to the end of your existing list, so this is how you add 'teach Python' to myGoals:
     
    >>> myGoals
    ['defeat foes', 'eat veal', 'make ladies swoon'] 
    >>> myGoals.append('teach python')
    >>> myGoals
    ['defeat foes', 'eat veal', 'make ladies swoon', 'teach python'] 
    
  • The .insert() method is similar to the "append" function, however "insert" requires that you designate a place that your new item should be inserted. This is done by typing the insert command in much the same way as the append command above, but also placing the target index BEFORE the string or item you wish to insert. For example, myGoals.insert(3,'brush teeth daily') will insert 'brush teeth daily' into the fourth spot on your list (remember again that indexing starts with 0 in Python):
     
    >>> myGoals.insert(3, 'brush teeth daily')
    >>> myGoals
    ['defeat foes', 'eat veal', 'make ladies swoon', 'brush teeth daily', 'teach python'] 
    
  • Predictably, the .remove() method allows you to remove an item from your list. It is typed listName.remove('item') in the same manner as the insert and append functions:
     
    >>> myGoals.remove('eat veal')
    >>> myGoals
    ['defeat foes', 'make ladies swoon', 'brush teeth daily', 'teach python'] 
    
  • You do not have to specify the placing of the item you wish to remove, however, understandably, Python cannot remove items that are not on the given list, and will return an error when told to do so.

Learn More

  • Note that .remove() removes only the first instance even when duplicates exist. Below, only the first 'b' is removed.
     
    >>> li = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] 
    >>> li.remove('b')
    >>> li
    ['a', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] 
    
  • It is also possible to delete an item based on its position. .pop() is the method to use. If the position is not specified, it removes the very last item. Otherwise it removes the item at the designated position and shifts up the remaining items. In either case, the removed item is returned. (.pop() is the only list method that modifies a list in place AND returns a value. See Mutability for the distinction.)
     
    >>> li = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
    >>> li.pop()
    'g' 
    >>> li
    ['a', 'b', 'c', 'd', 'e', 'f']
    >>> li.pop(2)
    'c' 
    >>> li
    ['a', 'b', 'd', 'e', 'f']
    
  • .extend() is another list method that's handy. It is used to extend a list by another list. Below, the list [1,2,3,4,5] is extended by three new items:
     
    >>> li = [1,2,3,4,5]
    >>> li.extend([6,7,8])
    >>> li
    [1, 2, 3, 4, 5, 6, 7, 8] 
    
    You must, however, take care NOT TO CONFUSE .extend() and .append(). Try li.append([6,7,8]) in the example above and see what happens. If you want to torture yourself further, try li.append('hello') and li.extend('hello'). (Hint: In the latter, Python is forced to interpret 'hello' as a list.)

Explore

  • Anne Dawson naturally has A LOT of examples on list. Search for "list". (NOTE: She names some of her lists simply list. DO NOT DO THIS: it overrides the built-in list() function.)
  • Think Python has a comprehensive chapter on lists: Ch.10 Lists. It starts from the concept and also explains the important methods.