10-09-2009, 08:45 PM
What is a list comprehension...? In a nutshell, you can think of it as a function, for loop, with if clauses that return a value(s) in a list, and only takes up one line.
What I'm basically getting it as something similar to this
def IsEven( Numbers ):
EvenNumbers = []
for x in Numbers:
if x % 2 == 0:
EvenNumbers.append(x)
return EvenNumbers
Even = isEven([100, 2, 7, 23567])
Kinda lengthy right? Why not cut it down to one line ;)
Even = [ x for x in [100, 2, 7, 23567] if x % 2 == 0 ]
Break it down a bit;
Even = [ x for x in [100, 2, 7, 23567] if x % 2 == 0 ]
x for x in [100, 2, 7, 23567]
The inital x is what will be returned, in a list, (If an if clause exists, and is returned as True) in which you can modify in anyway you could normaly modify how a function "returns" such as
x/3 for x in
or
True for x in
which would return the boolean True
The next part is the if or multiple if caluses, these are again optional. The return value will only be returned if the if clauses return true, or none exist.
For example a quick why to divide each element in a large list by 3 would be;
NewList = [x/3 for x in OldList]
To divide elements in the list who only end in 3 we could do something similar to this;
NewList = [x/3 for x in OldList if str( x )[ -1: ] == "3" ]
As previously mentioned, a List Comprehension will always return its value(s) in a list. Which can make it very accessible to another for loop. For example the problem...
can be solved very easily with List Comprehensions, and in very few lines.
Well that about sums it up... ^__^ and if you are like me and do not like using map, or lambda, you will love List Comprehensions
What I'm basically getting it as something similar to this
def IsEven( Numbers ):
EvenNumbers = []
for x in Numbers:
if x % 2 == 0:
EvenNumbers.append(x)
return EvenNumbers
Even = isEven([100, 2, 7, 23567])
Kinda lengthy right? Why not cut it down to one line ;)
Even = [ x for x in [100, 2, 7, 23567] if x % 2 == 0 ]
Break it down a bit;
Even = [ x for x in [100, 2, 7, 23567] if x % 2 == 0 ]
x for x in [100, 2, 7, 23567]
The inital x is what will be returned, in a list, (If an if clause exists, and is returned as True) in which you can modify in anyway you could normaly modify how a function "returns" such as
x/3 for x in
or
True for x in
which would return the boolean True
The next part is the if or multiple if caluses, these are again optional. The return value will only be returned if the if clauses return true, or none exist.
For example a quick why to divide each element in a large list by 3 would be;
NewList = [x/3 for x in OldList]
To divide elements in the list who only end in 3 we could do something similar to this;
NewList = [x/3 for x in OldList if str( x )[ -1: ] == "3" ]
As previously mentioned, a List Comprehension will always return its value(s) in a list. Which can make it very accessible to another for loop. For example the problem...
Quote:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
can be solved very easily with List Comprehensions, and in very few lines.
Code:
sum = 0
for y in [x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0]:
sum += y
print sum
Well that about sums it up... ^__^ and if you are like me and do not like using map, or lambda, you will love List Comprehensions
![[Image: nv70ad.png]](http://i27.tinypic.com/nv70ad.png)
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation