3

I understand that loops are a bad idea in python and I should avoid them.

Well, I have several of those I want to avoid.

I have a list of stuff named lipid:

class bead:
    x = 0
    y = 0
    z = 0
    LID = 0
    t = 0

class lipid:
    h = bead()
    b = bead()
    t = bead()
    LID = 0

and I am doing the following (code below):

  1. initializing a 2d array of hs going over all the lipids and
  2. determine if they are counted as U or down
  3. adding value to the appropriate h

How can I avoid, at least, the first loop?

1:

class h:
    cU = 0
    cD = 0
    hU = 0
    hD = 0
    h = 0

  for i in range(0,8):
        hs.append([])
        for j in range(0,8):
            index = (i,j)
            hn = h()
            hs[i].append(hn)

2 and 3:

 for LID in lipids:
        l = lipids[LID]
        up = l.h.z > l.t.z
        X = (int)(l.b.x*8/L)
        Y = (int)(l.b.y*8/L)
        Z = (l.b.z)*0.5
        if up:
            hs[X][Y].hU += Z
            hs[X][Y].cU += 1
        else:
            hs[X][Y].hD += Z
            hs[X][Y].cD += 1
Yotam
  • 10,295
  • 30
  • 88
  • 128
  • 7
    Who told you loops are bad? – ThiefMaster Jan 15 '12 at 12:25
  • 4
    `I understand that loops are a bad idea in python and I should avoid them.` -- wat? Where did you get that idea? Iteration is encouraged over recursion. Now, quite a few *simple* `for` loops can be replaced with syntactic sugar (a ways of doing the same thing with nicer source code). But those are just a better way to write loops. –  Jan 15 '12 at 12:25

2 Answers2

11

Loops are not a bad idea. It is just that loop-intensive code can be slow. But it is nothing specific to the loops, it is just that Python is not as fast in general as some other languages. I suggest you do not avoid loops if they are the most natural expression of your algorithm. If your code turns out slower of what you expect or need, then look for ways of optimizing it (profiling, to start with).

The article on Analysis of Algorithms in Wikipedia might me useful for you.

dsign
  • 12,340
  • 6
  • 59
  • 82
  • 4
    Even "loop-intensive code can be slow" doesn't make a lot of sense. If your algorithm is O(n), it's going to be O(n), regardless of whether the loop is implicit or not. And many, **many** problems require some O(n) algorithm. If you push a loop into optimized C code, the constant factors are a bit lower, but it's still a loop (though it *is* a valid optimization if you're that desperate). –  Jan 15 '12 at 12:49
  • 1
    +1 to your comment @delnan. I'm editing my answer accordingly. – dsign Jan 15 '12 at 14:42
2

An example of how to create the 2d array with list comprehensions would be as follows:

hs = [[h() for i in range(8)] for j in range(8)]

However, as noted in the comments, this wouldn't be different than writing for loops since list comprehensions are syntactic sugar. Use it just if it's more convenient in terms of readability and maintenance.

Note: As pointed out by a comment, one additional benefit from list comprehensions is that they usually provide better perfomance than for loops and the map function.

jcollado
  • 39,419
  • 8
  • 102
  • 133
  • 5
    _this wouldn't be different than writing for loops since list comprehensions are syntactic sugar._ - This is not true, list comprehensions are faster than a for loop as the loop is implemented at a C level, not a Python level. They are also faster than the map() function. – Gareth Latty Jan 15 '12 at 12:55
  • @Lattyware Thanks for your comment. I've looked into this and while you're completely right for most cases, it seems there are still some of them in which `map` is faster than list comprehensions as explained [here](http://stackoverflow.com/a/1247490/183066). – jcollado Jan 15 '12 at 13:28