-2

Why does the if != j not need to know what list it's accessing unlike list [i]? If it does already know how so? Why can't I just do if i + j == t because the i and j variable already work when checking if i != j. I know list[i] accesses the list index but don't if i + j == t work too? Please explain in simple words. A kind answer is appreciated.

list = [3, 9 , 23, 4, 2]
t = 12

for i in range(len(list)):
  for j in range(len(list)):
    if i != j:
      if list[i] + list[j] == t:
        print(list[i] + list[j])

I tried changing it so that list[i] was written as i but it didn't work. The console was empty. I expected it to show the desired output(self explanatory).

  • 4
    `i != j` simply compares two numbers. `list[i]` and `i` are two different things, just like "the second element of the list" and "two" are two different things. I'm having trouble understanding the rest of the question; can you rephrase it clearly? – nanofarad Mar 27 '23 at 03:26
  • `i` and `j` are index values, `list[i]` and `list[j]` are the values in the list corresponding to those indexes. In your example, there is no combination of `i` and `j` which add up to 12, because both `i` and `j` max out at 4. Also, I would recommend not using `list` as a variable name as it is an existing keyword that you would be overwriting. – Shorn Mar 27 '23 at 03:26
  • Explain how they're different? – ineedstackoverflowplz Mar 27 '23 at 03:26
  • With the list given in your example, `list[2]` is 23. It's element number 2 in the list (noting that we could 0, 1, 2, ... to identify elements in a list). Meanwhile, `2` is just 2. – nanofarad Mar 27 '23 at 03:28
  • `i != j` has nothing to do with any list. These variables are used as list indexes, and the point is to avoid ever using the same list element twice in the sum. For example, if `t` was 6, that would have to be from the 4 and the 2, you couldn't use the 3 twice. – jasonharper Mar 27 '23 at 03:28
  • Rephrase of my question to nanofarad: How does the if i!=j part understand that it's going to check the indexes in the bruh list without any clear instructions but the list[i] + list[j] needs it? – ineedstackoverflowplz Mar 27 '23 at 03:42
  • Question for shorn: i and j do max out at 4 but the code works to display the desired output if that's the case. And 9 and 3 add up to 12? – ineedstackoverflowplz Mar 27 '23 at 03:44
  • 2
    Welcome to Stack Overflow. "Why does the if != j not need to know what list it's accessing unlike list [i]?" This question does not make sense, because the code `if i != j` **is not accessing a list**. If the question is "how does the `for` loop know how many times it should run?", that is because the values come from `range(len(list))`, which has the same length that the original `list` does. By the way, [don't use that as a variable name](https://stackoverflow.com/questions/20125172). – Karl Knechtel Mar 27 '23 at 03:47
  • 2
    "How does the if i!=j part understand that it's going to check the indexes in the bruh list" - it **doesn't, and there is no logical reason why it would need to**. – Karl Knechtel Mar 27 '23 at 03:50
  • FYI: `list` is the name of a pre-existing type in Python and shouldn't be used as a variable name. – Mark Tolonen Mar 27 '23 at 03:52
  • 1
    "Why can't I just do if i + j == t?" Because that means something **completely different and unrelated**. It is the same as how, if you take a picture of the house at 1 Any Street and then another picture of the house at 3 Any Street and put them side by side, that does not make a picture of the house at 4 Any Street. – Karl Knechtel Mar 27 '23 at 03:54
  • Yup, I realized I overthank way too much and understand it now. – ineedstackoverflowplz Mar 27 '23 at 04:16

2 Answers2

0

As written, your program can be replaced by this:

print(12)
print(12)

But that is because there is no input and no change to anything, and only one combination of i and j where the result is 12: one where i is 0 and j is 1, and one where i is 1 and j is 0.

To explain what is going on, though, try changing your program to this:

list_of_int = [3, 9 , 23, 4, 2]
t = 12

for i in range(len(list_of_int)):
  for j in range(len(list_of_int)):
    print("currently, i is ", i)
    print("currently, j is ", j)
    print("currently, i points to ", list_of_int[i])
    print("currently, j points to ", list_of_int[j])
    if i != j:
      print("i != j")
      if list_of_int[i] + list_of_int[j] == t:
        print(list_of_int[i] + list_of_int[j])

Using i + j == t won't work, since t is 12, and both i and j have a maximum of 4.

Essentially, what is happening is that the value of i changes from 0 to 4, the value of j changes from 0 to 4, with the value of list_of_int[i] (and also list_of_int[j]) being 3, 9, 23, 4, and 2 in sequence.

enkorvaks
  • 124
  • 1
  • 1
  • 2
-1

i and j are the indices (positions) within the list

When you test for i != j, you are just saying:

If I am not looking at the same position in the list:

At that stage, you have not yet looked at what values are in those two positions.

For example, if the list is [10,20,30,40], and you are looking at the 20 and 30, the values of i and j would be just 1 and 2.

Index Value
0 10
1 20
2 30
3 40

If you loop indices i and j, you would first check that i and j are different, so that you can notice you are checking the same value twice, before you look at the values.

During that comparison, the program does not understand that i and j will be indexes into a list

That understanding happens in your head as a programmer.

The program only understands that they are indices into a list when it comes to the list[i] and list[j] calls.

ProfDFrancis
  • 8,816
  • 1
  • 17
  • 26
  • Don't use code formatting for English prose. Use `>` to create a quoted block in markdown for English prose. (Also, `position` is terminology, not code, so it shouldn't be code-formatted either). – Charles Duffy Mar 27 '23 at 03:46
  • 1
    See [When should code formatting be used for non-code text?](https://meta.stackoverflow.com/questions/254990/when-should-code-formatting-be-used-for-non-code-text) on [meta]. – Charles Duffy Mar 27 '23 at 03:47
  • I understand that but how does i != j understand that it's looking through "list". Meanwhile list[i] + list[j] needs the list name("list") before [i] or [j] variables to understand it's checking in "list". – ineedstackoverflowplz Mar 27 '23 at 03:48
  • 2
    @ineedstackoverflowplz, it _doesn't_ understand that and it doesn't _need to_ understand that. It's just comparing the number stored in i and the number stored in j with no reference to the list at all. The code in question (`i != j`) is not expected or intended to interact with the list in any way. – Charles Duffy Mar 27 '23 at 03:48
  • @ineedstackoverflowplz It _doesn't_. – Alexander Mar 27 '23 at 03:48
  • @CharlesDuffy So basically it's comparing the iteration over the list and checking if they're not the same iteration. And if they're not the same it's executing the final block? – ineedstackoverflowplz Mar 27 '23 at 03:54
  • It's not "the iteration". They're literally just **numbers**. The code `print(list[i] + list[j])` **uses** those numbers to get values out of `list`. – Karl Knechtel Mar 27 '23 at 03:55
  • 1
    Yes! It's comparing the _index_ numbers `i` and `j`, without knowing they are index numbers. – ProfDFrancis Mar 27 '23 at 03:56
  • @ineedstackoverflowplz, it's not an "iteration over the list" until you use `list[i]`. Until you've done that it's just the number `i`. – Charles Duffy Mar 27 '23 at 04:01
  • Yup, I realized I overthank way too much and understand it now. – ineedstackoverflowplz Mar 27 '23 at 04:16