Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-24-2004, 06:26 AM   #1 (permalink)
inkedmn
Registered User
 
Join Date: Mar 2004
Location: Fullerton, CA
Posts: 26
inkedmn is on a distinguished road
Send a message via AIM to inkedmn
Python Lists Overview

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:
Code:
>>> mylist = []
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:
Code:
>>> x = list()
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:
Code:
>>> mylist = []
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:
Code:
>>> mylist
['foo']
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
inkedmn is offline   Reply With Quote
Old 03-24-2004, 07:01 PM   #2 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Nice job. From what I can gather from a little reading I've been doing on Python, a list appears to be analogous to what would be referred to as a 'numerically indexed array' where a dictionary is basically an 'associative index array'. About the only thing I would add is an example of how to loop through a list like so:
Code:
>>> mylist = ['one', 'two', 'three']
>>> for x in range(len(mylist))
...  print mylist[x],
...
one two three
Are there any other usefull methods for iterating through a list aside from a simple for loop?
bdl is offline   Reply With Quote
Old 03-24-2004, 10:22 PM   #3 (permalink)
inkedmn
Registered User
 
Join Date: Mar 2004
Location: Fullerton, CA
Posts: 26
inkedmn is on a distinguished road
Send a message via AIM to inkedmn
Code:
>>> x = ['one','two','three']
>>> for i in x: print i,
one two three
this works too
inkedmn is offline   Reply With Quote
Old 03-25-2004, 01:38 PM   #4 (permalink)
inkedmn
Registered User
 
Join Date: Mar 2004
Location: Fullerton, CA
Posts: 26
inkedmn is on a distinguished road
Send a message via AIM to inkedmn
[edit - sorta double-posted]
oh, and specifically didn't introduce looping because, while i agree that it's essential to harnessing the power of lists, it's a bit more complicated to understand than a list on its own. List comprehensions are really cool too, but i omitted those as well (for the same reason).
inkedmn is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python class definitions MealMan401 All Other Coding Languages 4 05-22-2004 01:17 PM
New Tutorial: Python Lists Overview sde Code Newbie News 3 03-24-2004 09:54 AM
PyCon 2004 - Python Developers Conference sde Code Newbie News 0 02-12-2004 03:45 PM


All times are GMT -8. The time now is 12:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting