0

I have part of code that looks like this:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[i+1]))

For the first iteration, I want it to do,

print('{} used {} words that {} did not use.'.format(a[0], 50 , a[1]))

but for the second iteration, I want this:

print('{} used {} words that {} did not use.'.format(a[1], 50 , a[0]))

How can this be done?

5
  • 1 - i is one option. Commented Nov 30, 2017 at 7:12
  • (i+1)%2 is another Commented Nov 30, 2017 at 7:13
  • 1
    i ^ 1 also works Commented Nov 30, 2017 at 7:14
  • 2
    I don't think it's really clear what is being asked here. How should the desired solution generalize to longer loops, for example iterating over [0,1,2,3,4]? Or, Eric, is [0,1] the only case you want to handle? If so, why not just write two print() statements and be done with it? Commented Nov 30, 2017 at 7:15
  • I’d say that if it’s only two items then remove the loop for readability. Commented Nov 30, 2017 at 7:17

4 Answers 4

2

You can use the modulous operator %:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i % 2], 50 , a[(i + 1) % 2]))

On the first iteration, i = 0:

i % 2       == 0 % 2 == 0
(i + 1) % 2 == 1 % 2 == 1

On the second iteration, i = 1:

i % 2       == 1 % 2 == 1
(i + 1) % 2 == 2 % 2 == 0

Note that the first i % 2 == i for this particular instance of your problem.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use the indice modulo 2 (%2):

a = ['first', 'second']

for idx in [0, 1]:
    print('{} used {} words that {} did not use.'.format(a[idx%2], 50 , a[(idx+1)%2]))

output:

first used 50 words that second did not use.
second used 50 words that first did not use.

Alternatively, if only two items:

it might be easier to read and maintain to do like this:

a = ['first', 'second']
x, y = a
print('{} used {} words that {} did not use.'.format(x, 50 , y))
print('{} used {} words that {} did not use.'.format(y, 50 , x))

Comments

1

If that is exactly what you're looking for, you could do:

for i in [0,1]:
    ...
    print('{} used {} words that {} did not use.'.format(a[i], 50 , a[(i+1)%2]))

Comments

0

This is my solution:

a = ['John','Doe']
amount = 50

# Use index to create strings to be formatted
s1 = '{0} used {2} words that {1} did not use.'
s2 = '{1} used {2} words that {0} did not use.'

print(s1.format(*a,amount))
print(s2.format(*a,amount))

Returns:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.

Or :

# Use index to create strings to be formatted
s = '''\
{0} used {2} words that {1} did not use.
{1} used {2} words that {0} did not use.'''

print(s.format(*a,amount))

Returns:

John used 50 words that Doe did not use.
Doe used 50 words that John did not use.

Comments

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.