Introduction to Python List Comprehensions - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32) +---- Thread: Introduction to Python List Comprehensions (/showthread.php?tid=988) Pages:
1
2
|
Introduction to Python List Comprehensions - Fallen - 10-09-2009 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. can be solved very easily with List Comprehensions, and in very few lines. Code: sum = 0 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 RE: Introduction to Python List Comprehensions - Nyx- - 10-09-2009 sheesh your math teacher must love you ^^ RE: Introduction to Python List Comprehensions - Socrates - 10-09-2009 Yea lol mine hates me. RE: Introduction to Python List Comprehensions - d2ax5n - 10-10-2009 I always get suspension from mines, gotta love it. RE: Introduction to Python List Comprehensions - uber1337 - 02-18-2010 map/filter equivalent: Code: sum = [] 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]) RE: Introduction to Python List Comprehensions - Fallen - 04-05-2010 (02-18-2010, 09:45 PM)uber1337 Wrote: map/filter equivalent: Lol, you and your lambda bologna RE: Introduction to Python List Comprehensions - GameOver* - 04-06-2010 hmm nice share dude! mile: thanks RE: Introduction to Python List Comprehensions - Fallen - 04-06-2010 (02-18-2010, 09:45 PM)uber1337 Wrote: map/filter equivalent: One liner FTW! Code: print sum([x for x in xrange(1000) if x % 3 == 0 or x % 5 == 0]) RE: Introduction to Python List Comprehensions - uber1337 - 04-06-2010 (04-06-2010, 04:17 PM)Fallen Wrote: One liner FTW!Must you shorten everything I write? RE: Introduction to Python List Comprehensions - Fallen - 04-07-2010 (04-06-2010, 05:38 PM)uber1337 Wrote: Must you shorten everything I write? I get bored.... tbh |