1

I'm starting to work and getting more familiar with Python in the context of data analytics and just got to know about list comprehensions which I find really elegant.

Although I'm aware there ist the risk of falling into love with it too much at the cost of readabilty, I really didn't find any guidelines or rules of thumb of when or when not to use it.

In my own code I used this generator expression inside a recursive function --> find_children() is a user definded function to create hierarchical data

(find_children(ancestor) for ancestor in parents if len(parents) > 0)

instead of

if len(parents) > 0:
    for ancestor in parents:
        find_children(ancestor)

It looks more neat but that's actually the only reason for me.

So I would like to collect opinions of maybe some experienced Pythonistas and their approach regarding this or what best practice is.

Matt444
  • 47
  • 9
  • 3
    *Don't* use iterables just for the side effect of calling a function. – chepner Mar 13 '20 at 13:18
  • 3
    Out of context, your generator expression doesn't *do* anything, as you aren't iterating over it. It just creates a generator that is immediately discarded without ever calling `find_children`. – chepner Mar 13 '20 at 13:19
  • 3
    Use a list comprehension when you want a list, not when you just want to squash a normal `for` loop on to one line. – chepner Mar 13 '20 at 13:20
  • Ok, so you don't do if for the sake of creating a one-liner if the Code is simple, I understand. In my example it does do the same thing as the snippet below effectively though. It is more a porcedure than a function, doesn't return anything. – Matt444 Mar 13 '20 at 13:23
  • try reading this https://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops – Hari Mar 13 '20 at 13:27
  • 1
    Your translation from list comprehension to regular `for` loop is not correct. The (optional) `if` in a list comprehension *filters* the output from an iterable. See here: https://stackoverflow.com/a/15474952/1643973 – jjramsey Mar 13 '20 at 13:41

1 Answers1

1

List comprehensions are idiomatic python code and as such perfectly fine. Their main competitor is a functional style using map(), filter() etc. Both style are perfectly fine. I would probably refrain from using list comprehensions when you have long statements, e.g. x["data"]["data"]["data"] for x in something] asks for trouble.

Also nested list comprehensions are evil, I personally see them as a code smell due to their poor readability.

Finally, the goal of good code is to be readable - when you finished a piece of code, step back and try to judge who readable your code really is. Introduce functions and variables to increase readability. List comprehensions are just a part of that picture.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
  • Could you address the example in the question? This is decent general advice but it doesn't address any of the problems with how the OP is using them. – John Kugelman Mar 13 '20 at 13:50