Python, Lists, and You...
A python list is a special thing. It's most closely analagous to a C++ vector or a Java ArrayList, rather than any old array. Python lists are dynamically resized and can contain heterogeneous types (i.e., one python list could contain a string, an integer, and another list). Since python is dynamically typed, you don't have to worry about type declarations or any of that other stuff, you just create your list and go.
Now, let's fire up the python interpreter and see what these lists are all
about. Now, there are several ways to create a list:
First, you can just declare an empty list:
or you can declare a list that actually contains elements:
Code:
>>> mylist2 = [1, "foo", 2.3]
* you'll notice that mylist2 contains an integer, a string and a float
you can also create a list by actually creating a new instance of the list
object:
and there are actually a few other ways, but we'll leave it at these three
right now.
Ok, so we've seen a couple ways lists are created. Let's start out with an empty
list like so:
Now, say we want to add an element to the list (remember, type is irrelevant).
How can we do it? Well, the first way is to use the 'append' method:
Code:
>>> mylist.append("foo")
If we just type the name of our list in the interpreter, it'll spit out the
current list in a pretty string representation:
Isn't this easy?
you can also use the 'insert' method to add items to a list. this method
takes two arguments: the index you want to put the element
before and
the element itself, like this:
mylist currently has one element, 'foo'. Let's add some more elements just to
illustrate this point:
Code:
>>> mylist.append("foo")
>>> mylist.append("foo")
>>> mylist.append("foo")
>>> mylist.append("foo")
>>> mylist.append("foo")
so now, mylist will look like:
Code:
>>> mylist
['foo', 'foo', 'foo', 'foo', 'foo', 'foo']
now, let's say i want to insert 'bar' before the second element in the list.
to do that, i'd do:
Code:
>>> mylist.insert(1, "bar")
>>> mylist
['foo', 'bar', 'foo', 'foo', 'foo', 'foo', 'foo']
it's as simple as that
Now, what if we want to remove something? Again, easy as pie, we'll just use
'remove', which removes the first instance of whatever you pass it, like this:
Code:
>>> mylist.remove("foo")
>>> mylist
['bar', 'foo', 'foo', 'foo', 'foo', 'foo']
you'll notice that the first 'foo' in the list is now gone.
next, we'll show how to get a count of how many times something occurs in a
list. Let's try it, using 'count':
Code:
>>> mylist.count("foo")
5
>>> mylist.count("baz")
0
So, our list has 5 instances of 'foo' in it, and zero instances of 'baz', easy
enough?
ok, just a couple more basic list operations we can do. next we'll show how
to get the index for the first occurence of something. Say we want to find
out where in the list 'foo' first appears, we'd do:
Code:
>>> mylist.index("foo")
1
and there it is, 'foo' first appears at index 1 (the second element, as python
lists are zero-indexed)
the last two things we'll be covering are the reverse and sort methods, which are fairly self explanatory; one reverses a list, the other sorts it
Code:
>>> x = [2, 6, 1, 9, 3, 1, 9, 6]
>>> x.sort()
>>> x
[1, 1, 2, 3, 6, 6, 9, 9]
>>> x.reverse()
>>> x
[9, 9, 6, 6, 3, 2, 1, 1]
the only thing you need to remember about sort and reverse is that they
perform the operation on the list
in place, meaning they don't return a
sorted/reversed copy of the list, they sort/reverse the actual list.
Anyway, that's about it. Now, this is just the tip of the iceberg, python
lists can do plenty more than what we've seen here, so head over to
python.org
and continue learning
Feel free to post questions