1

struggling a bit understanding how to access indices in Python loops. How would you write this code in Python?

int x = 5;
for (int i = 0; i < s.length; i++) {
    if (s[i] + s[i - 1] == x) {
        System.out.println("Success");
    }
}

So far, I have tried the enumerate method, but I don't think it's working as intended.

x = 5
for i, c in enumerate(s):
    if (i, c + (i - 1), c == x):
        print("Success")

Sorry if this has been asked before, but I couldn't really find a solution to this exact way of handling indices in Python loops. Would really appreciate the help.

5
  • 2
    x = 5 for i in range(len(s)): if s[i]+s[i-1] == x: print('Success') Commented Sep 17, 2018 at 11:23
  • 1
    What is s in your question ? Commented Sep 17, 2018 at 11:25
  • 1
    Is it a bug or a feature that the pair of first and last elements of s are tested together? Commented Sep 17, 2018 at 11:26
  • What is the error you get? What do you expect instead? Commented Sep 17, 2018 at 11:26
  • s is a list, or an array in the Java code Commented Sep 17, 2018 at 11:34

3 Answers 3

2

considering s to be a sequence, assuming is to be a sequence with numbers and considering the first Java-style code in the first part

s = [0,1,2,3,4,5,..]
x = 5

for i in range(1, len(s)):
    if s[i] +s[i-1] == x:
        print("Success")
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the Java code starts at 0 and not 1, so it checks if the first and last elements sum up to 5.
0
for i, c in enumerate(s):
    if c + s[i - 1] == x:

c here will be an element from the list referring to s[i] and i will be index variable. In order to access the element at i-1, you need to use s[i - 1]. But when i is 0, you will be comparing s[0] with s[-1] (last element of s) which might not be what you want and you should take care of that.

2 Comments

Try this values: s=[0,0,5,5,6,5] and x=5, it should print one time, but it will print two times, that is because of s[i -1] in the first iteration.
I mentioned that in the answer that it might not be what you want to do during the first iteration as i will be 0. The Java code in the question was starting the loop from 0 and I didn't want to deviate from that.
0

the enumerate() method splits the lop target in a tuple containing the index, and the element of the target. Unlike c style languages, when you use a for loop in python without the enumerate, i represents the element. This means that you do not have to insert the index in the list when calling it. Your code in python would be like this. s = [1, 2, 3, 4, 5, 6, 7, 8, 9] # the list named s. x = 5 for i in s: if i + i-1 == x: print("success")

Since we're only working with integers, you could do like this as well

x = 5 for i in range(10): if i + i-1 == x: print("success")

1 Comment

I want to access the number in the i-th index, not the i-th index itself. In your example, aren't we adding 1 + 0, then 2 + 1, and so on?

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.