0

Is there a better or cleaner way to achieve the results below? I have 4 conditions and half of the code is filled with "while-loop", i += 1, and j += 1.

i = 0
j = 0
limit1 = 2
limit2 = 4

while (i < limit1 and j < limit2):
    lists.append ('First index: %d, Second index: %d' % (i, j))
    j += 1
    lists.append ('First index: %d, Second index: %d' % (i, j))
    i += 1
    j += 1

for i in lists:
    print (i)

Results:

First index: 0  Second index: 0
First index: 0  Second index: 1
First index: 1  Second index: 2
First index: 1  Second index: 3
3
  • The name lists says that that variable consists of multiple lists, but it doesn't. It's just a list of ints. It'd probably be a good idea to use a different name. Commented Jun 17, 2015 at 1:57
  • @user2357112 Well, it's a list of strings that could possibly be described as "lists". Commented Jun 17, 2015 at 2:03
  • Whoops, list of strings, not list of ints. In any case, calling strings lists is a bad idea; they're not lists, and they behave very differently from lists. Commented Jun 17, 2015 at 2:04

2 Answers 2

2

It's quite straightforward to just compute j from i:

for i in xrange(limit1):
    l.append('First index: %d, Second index: %d' % (i, 2*i))
    l.append('First index: %d, Second index: %d' % (i, 2*i+1))

This assumes limit2 is twice limit1. If that isn't always the case, you can add an additional check:

for i in xrange(limit1):
    if 2*i >= limit2:
        break
    l.append('First index: %d, Second index: %d' % (i, 2*i))
    l.append('First index: %d, Second index: %d' % (i, 2*i+1))

or compute which limit to use up front:

for i in xrange(min(limit1, (limit2 + 1)//2)):

though as you can see, the limit computation may be error-prone.

Note that if limit2 isn't a multiple of 2, your code may emit an entry for j == limit2:

>>> lists = []
>>> i = 0
>>> j = 0
>>> limit1 = 2
>>> limit2 = 3
>>> while (i < limit1 and j < limit2):
...     lists.append ('First index: %d, Second index: %d' % (i, j))
...     j += 1
...     lists.append ('First index: %d, Second index: %d' % (i, j))
...     i += 1
...     j += 1
...
>>> for i in lists:
...     print (i)
...
First index: 0, Second index: 0
First index: 0, Second index: 1
First index: 1, Second index: 2
First index: 1, Second index: 3

If this isn't desired, we can rearrange the loop to go by j instead of i:

for j in xrange(min(limit2, limit1*2)):
    l.append('First index: %d, Second index: %d' % (j//2, j))
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't necessarily break when j is greater than limit2.
@DTing: Indeed. It assumes that limit2 == 2*limit1. I can make that assumption explicit and add a version where that assumption isn't made.
This is exactly what I'm looking for. Thank you for the detail break down. Time to review those Summation class materials.
1
lists = ['First index: %d, Second index %d' % (j//2, j) for j in range(limit2) if j//2 < limit1]

3 Comments

This does something completely different from what's desired.
You're right. I guess I'm not sure why what's desired is desired, but I'll edit my answer.
Better. It's important to note that if limit2 isn't a multiple of 2, this code won't emit an entry for j == limit2, while the question's code often will. Whether this is a bug in the original code is something only the questioner can tell us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.