0

I want to check if the index of list in my list is in same position, then after make the position same I want to print it

news_a = ['A', 'B', 'C', 'D']
news_b = ['E', 'F', 'G', 'H']
for i in news_a:
    for j in news_b:
        if i == j:
            print(f"{i}\n{j}")
  • `for i in news_a` will iterate over the _values_ of the list, so `i` will be `'A'`, `'B'`, and so on. it will not give you indices. – jkr Dec 11 '21 at 00:19
  • 1
    Welcome to Stack Overflow. I have absolutely no idea what you're trying to ask. For the given input, what should the exact output be? (If that output is not interesting, edit the input so that you can have output that explains what the code is supposed to do.) – Karl Knechtel Dec 11 '21 at 00:19
  • Could you please elaborate more on what exactly you mean by "index of the list"? The code you have is just going through both arrays of strings and checks each string. – Nijat Mursali Dec 11 '21 at 00:22
  • Welcome to Stack Overflow! Please take the [tour]. Please [edit] to clarify what you're trying to accomplish exactly. For more tips, see [ask]. – wjandrea Dec 11 '21 at 00:26
  • 2
    Is this what you're looking for? [How to iterate through two lists in parallel?](/q/1663807/4518341) – wjandrea Dec 11 '21 at 00:26
  • 1
    You should clarify what you want to achieve, but look up `enumerate` and `list.index`, and I think you'll find what you need. – sal Dec 11 '21 at 00:27

1 Answers1

0

If I'm understanding your question correctly, all you have to change is that you need to reference the index of each element in your for-loop iterations, and the elements themselves when printing them.

news_a = ['A', 'B', 'C', 'D']
news_b = ['E', 'F', 'G', 'H']
for i in range(len(news_a)):
    for j in range(len(news_b)):
        if i == j:
            print(f"{news_a[i]}\n{news_b[j]}")
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Stoic
  • 945
  • 12
  • 22