Usefullness of one-line statements in Python
Question: Is using one line loops, even nested loops always a good practice in Python? I
see a lot of people just love "one-liners", but to me they're hard to read
sometimes, especially if we're talking about nested loops.

Moreover most of nested loops I've seen so far exceed the recommanded 79
characters per line.

So I'd like to know if "one-liners" offer anything extra apart being compact?
Do they use less memory maybe?


Answer: Yes, they may be easily faster, since more code may be run in C (in explicit
loop all of immediate steps has to be available to interpreter, in one-liner
list comprehension it does not have to). There is also overhead of `.append`
method call, method look-up etc. In list comprehension all of that is avoided:

    
    
    import timeit
    
    
    def f1():
        result = []
        for x in range(100):
            for y in range(100):
                result.append(x * y)
        return result
    
    
    def f2():
        return [x * y for y in range(100) for x in range(100)]
    
    
    print('loop: ', timeit.timeit(f1, number=1000))
    print('oneliner:', timeit.timeit(f2, number=1000))
    

Results (Python 3):

    
    
    loop:  1.2545137699926272
    oneliner: 0.6745600730064325
    

