> [Bill]
> I've seen list comprehensions, but I didn't understand how they worked.
> Since I understand how my function works, it's not so hard to figure out
> what your doing with this list comp. I guess the brackets ([]) signify a
list.
> Then the for loop is placed on the right-hand side and then on the left,
you
> append to "the list". I think the other list comprehensions I saw before
> were performing more operations, but this one is not that hard to figure
out.

You're thinking right. The syntax is supposed to be something like

[ altereditem for item in iterable if condition ]

So,
to get all the even numbers between 1 & 10

>>> a = [x for x in range(1,11) if x % 2 == 0]
>>> print a
[2,4,6,8,10]

For a much, much better explanation, I recommend Alan Gauld's tutorial, the
section which is relevant can be found here
http://www.freenetpages.co.uk/hp/alan.gauld/tutfctnl.htm

HTH,
Jacob Schmidt

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to