Support Forums

Full Version: Introduction to Python List Comprehensions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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...

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
sheesh your math teacher must love you ^^
Yea lol mine hates me.
I always get suspension from mines, gotta love it.
map/filter equivalent:
Code:
sum = []
for x in range(1000):
    sum.append(x)
sum = filter(lambda x : x % 3 == 0 or x % 5 == 0, sum)
sum = reduce(lambda x, y: x + y, sum)
print sum
Yep. Your way is better ;)

Edit:
One liner FTW!
Code:
reduce(lambda x,y : x+y, [x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])
(02-18-2010, 09:45 PM)uber1337 Wrote: [ -> ]map/filter equivalent:
Code:
sum = []
for x in range(1000):
    sum.append(x)
sum = filter(lambda x : x % 3 == 0 or x % 5 == 0, sum)
sum = reduce(lambda x, y: x + y, sum)
print sum
Yep. Your way is better ;)

Edit:
One liner FTW!
Code:
reduce(lambda x,y : x+y, [x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])

Lol, you and your lambda bologna Tongue
hmm nice share dude! Confusedmile: thanks
(02-18-2010, 09:45 PM)uber1337 Wrote: [ -> ]map/filter equivalent:
Code:
sum = []
for x in range(1000):
    sum.append(x)
sum = filter(lambda x : x % 3 == 0 or x % 5 == 0, sum)
sum = reduce(lambda x, y: x + y, sum)
print sum
Yep. Your way is better ;)

Edit:
One liner FTW!
Code:
reduce(lambda x,y : x+y, [x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])

One liner FTW!
Code:
print sum([x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])
;3
(04-06-2010, 04:17 PM)Fallen Wrote: [ -> ]One liner FTW!
Code:
print sum([x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0])
;3
Must you shorten everything I write?
(04-06-2010, 05:38 PM)uber1337 Wrote: [ -> ]Must you shorten everything I write?

I get bored.... tbh
Pages: 1 2