1 2 3 4 5 6 7def getfactors ( dividend ): 'Return a list of all factors of the given number.' ' return n for n in range ( 1, dividend + 1 ) if dividend% n 0 Less code can mean more readable code, but not always.Whitespace is your friend, especially when you’re writing comprehensions.In general, I prefer to write most of my comprehensions spaced out over multiple lines of code using the indentation style above.I do write one-line comprehensions sometimes, but I don’t default to them. Writing ugly comprehensionsSome loops technically can be written as comprehensions but they have so much logic in them they probably shouldn’t be.Take this comprehension. 1 2for n in range ( 1, 11 ): print ( n )List comprehensions are for looping over an iterable and building up new lists, while for loops are for looping over an iterable to do pretty much any operation you’d like.When I see a list comprehension in code I immediately assume that we’re building up a new list (because that’s what they’re for).If you use a comprehension for a purpose outside of building up a new list, it’ll confuse others who read your code.If you don’t care about building up a new list, don’t use a comprehension.

Python Password List Generator

Using comprehensions when a more specific tool existsFor many problems, a more specific tool makes more sense than a general purpose for loop.But comprehensions aren’t always the best special-purpose tool for the job at hand.I have both seen and written quite a bit of code that looks like this. 1 2 3 4import csv with open ( 'populations.csv' ) as csvfile: lines = list ( csv. Reader ( csvfile ))Comprehensions are a special-purpose tool for looping over an iterable to build up a new list while modifying each element along the way and/or filtering elements down.The list constructor is a special-purpose tool for looping over an iterable to build up a new list, without changing anything at all.If you don’t need to filter your elements down or map them into new elements while building up your new list, you don’t need a comprehension: you need the list constructor.This comprehension converts each of the row tuples we get from looping over zip into lists. 1 2 3 4 5from itertools import chain def sumall ( numberlists ): 'Return the sum of all numbers in the given list-of-lists.' ' return sum ( chain. Fromiterable ( numberlists ))When you should use a comprehension and when you should use the alternative isn’t always straightforward.I’m often torn on whether to use itertools.chain or a comprehension.I usually write my code both ways and then go with the one that seems clearer.Readability is fairly problem-specific with many programming constructs, comprehensions included. Needless workSometimes you’ll see comprehensions that shouldn’t be replaced by another construct but should instead be removed entirely, leaving only the iterable they loop over.Here we’re opening up a file of words (with one word per line), storing file in memory, and counting the number of times each occurs.

1 2 3 4with open ( 'wordlist.txt' ) as wordsfile: for line in wordsfile: if 'z' in line: print ( 'z word', line, end = ' )There’s no reason to convert an iterable to a list if all we’re going to do is loop over it once.In Python, we often care less about whether something is a list and more about whether it’s an iterable.Be careful not to create new iterables when you don’t need to: if you’re only going to loop over an iterable once, just use the iterable you already have. When would I use a comprehension?So when would you actually use a comprehension?The simple but imprecise answer is whenever you can write your code in the below and there isn’t another tool you’d rather use for shortening your code, you should consider using a list comprehension.

1 2def sumofsquares ( numbers ): return sum ( n. 2 for n in numbers )So in addition to the “can I copy-paste my way from a loop to a comprehension” check, there’s another, fuzzier, check to consider: could your code be enhanced by a generator expression combined with an iterable-accepting function or class?Any function or class that accepts an iterable as an argument might be a good candidate for combining with a generator expression. Please enable JavaScript to view theHi! My name is Trey Hunner.I help Python teams write better Python code through.I also help individuals level-up their Python skills with.

Python Password List Generator

Write Pythonic codeThe best way to improve your skills is to write more code, but it's time consuming to figure out what code to write.I've made to help solve this problem.Each week you'll get an exercise that'll help you dive deeper into Python and carefully reflect on your own coding style.The first 4 exercises are free.Sign up below for four free exercises!

Comments are closed.