Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Introduction to Python List Comprehensions
#1
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
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#2
sheesh your math teacher must love you ^^
[Image: sig.php]
Reply
#3
Yea lol mine hates me.
Reply
#4
I always get suspension from mines, gotta love it.
Reply
#5
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])
[Image: izsyo6.jpg]


Reply
#6
(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
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#7
hmm nice share dude! Confusedmile: thanks
Reply
#8
(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
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply
#9
(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?
[Image: izsyo6.jpg]


Reply
#10
(04-06-2010, 05:38 PM)uber1337 Wrote: Must you shorten everything I write?

I get bored.... tbh
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 1,725 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 8,239 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,159 12-11-2010, 04:13 PM
Last Post: Bursihido
  Python help Kharnage 2 733 02-12-2010, 09:07 PM
Last Post: Kharnage
  "==" and "is" in Python Canoris 1 726 02-07-2010, 03:55 PM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)