0

I have a list of lists and i am trying to loop through to create a new value in the second list based on two elements with the second list.

for line in input_list[1:]:
i = 0
for element in line:
    if i == 13:
        if line[7] > line[8]:
            line[13] == 1
        else:
            line[13] == 0
    i += 1

I am trying to set the value of line[13] based on the condition that line[7] is greater than line[8].

The code does not flag any errors, so syntactically it is correct, but when i print for the new list, it does not display any values (0 or 1) for line[13].

Any help would be much appreciated.

Thanks,

Ed

3
  • can you please show me your list of data, that can help me to solve this. Commented Jun 15, 2017 at 6:13
  • == tests for equality, use a single = for assignment. Commented Jun 15, 2017 at 6:49
  • I do strongly recommend using a checker like "pylint". It would warn you with: Statement seems to have no effect (pointless-statement). Commented Jun 15, 2017 at 6:52

3 Answers 3

3

Use line[13] = 1 instead of line[13] == 1. == is for comparison.

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

Comments

1

It will not show the correct output because you haven't use assignment operator . use line[13] = 1 instead of line[13] == 1 Example :- list= [[1,2],[3,4,5]]

For this list -

for line in list[1:]:
  i=0
  for element in line:
    if i==1:
       if line[0]>line[1]:
          line[1]=1
       else:
          line[1]=200
    i=i+1

It will work

Comments

0
for line in input_list[1:]:
    i = 0
    for element in line:
        if i == 13:
            if line[7] > line[8]:
                line[13] = 1
            else:
                line[13] = 0
        i += 1

code updated

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.