# Index of duplicate items between two list in python

**URL:** <https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122>\
**Category:** General\
**Created:** [May 3, 2020, 4:31pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122 "2020-05-03T16:31:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [May 3, 2020, 4:31pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/1 "2020-05-03T16:31:28Z")

</div>

I have two list `A` and `B`. I have to print those elements index number(index number+1) of list `A` ,which elements also exist in list `B`. For Every elements of `B` list will print in new line.

`Len(B) <=100`

```auto
A = [['a'], ['a'], ['b'], ['a'], ['b']]
B = [['a'], ['b']]

```

I have done so far:

```auto
for i,x in enumerate(A):
        if B[0] == x:
            print(i+1,end=" ")

```

**My output:**

```auto
1 2 4 

```

**Expected Output:**

```auto
1 2 4
3 5

```

How can i fix this?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 3, 2020, 4:37pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/2 "2020-05-03T16:37:02Z")

</div>

You could make a outer loop that iterates over the elements of B

---

<div class="post-metadata">

**Author:** ![borispry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/borispry/32/146326_2.png) [@borispry](https://forum.freecodecamp.org/u/borispry)\
**Post date:** [May 3, 2020, 4:43pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/3 "2020-05-03T16:43:34Z")

</div>

you have 2D arrays, (matrixes/ array of arrays), is that what you meant?  
a list would be:  
A = [‘a’, ‘b’]  
and you have (array of arrays)  
A = [[‘a’], [‘b’]]  
do you see the difference?  
and then you can loop thorough A and just check “if A[i] in B”.  
does this make sense?

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [May 4, 2020, 4:34am UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/5 "2020-05-04T04:34:44Z")

</div>

Can you please , give an example?

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [May 4, 2020, 4:38am UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/6 "2020-05-04T04:38:37Z")

</div>

**Did you mean this:**

```auto
A = ['a', 'a', 'b', 'a', 'b']
B = ['a', 'b']

for i in (A):
    if i in B:
        print(A.index(i)+1,end=" ")

```

**But Output is:**

`1 1 3 1 3`

**I need ,**

```auto
1 2 4
3 5

```

Every time it catches first `'a'` element of list A ☹

---

<div class="post-metadata">

**Author:** ![borispry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/borispry/32/146326_2.png) [@borispry](https://forum.freecodecamp.org/u/borispry)\
**Post date:** [May 4, 2020, 5:04am UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/7 "2020-05-04T05:04:59Z")

</div>

ok, i see. so it is just an array and not an array of arrays. good. so now it looks like you need to keep track of the position of the elements from B in A. First you need to keep track of things you find, so create C which will actually be an array of arrays. For each element in B you will create row in C and add every index from A to that row. so in the end your matrix C will look like:

```auto
[[1], [2], [4]]
[[3], [5]]

```

Also suggest to reverse the logic and start going through B first. While going through the first element in B, go through every element in A and if B[i] == A[j] add a new element in C[i].  
how is that? I’m trying to describe the logic instead of writing code for you, but i can do that as well, if you prefer.

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [May 4, 2020, 5:30am UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/8 "2020-05-04T05:30:27Z")

</div>

I understand,but cannot apply logic in code.Can you please give the code,then i will try to understand what’s going on 😞

---

<div class="post-metadata">

**Author:** ![borispry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/borispry/32/146326_2.png) [@borispry](https://forum.freecodecamp.org/u/borispry)\
**Post date:** [May 4, 2020, 3:45pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/9 "2020-05-04T15:45:05Z")

</div>

there are multiple approaches obviously, it all depends on the sophistication of loops and python in general. here’s a simple and elegant solution i ended up with

```auto
a = ['a', 'a', 'b', 'a', 'b']
b = ['a', 'b']

for index_b, item_b in enumerate(b):
    for index_a, item_a in enumerate(a):
        if item_b == item_a:
            print(index_a + 1, end=" ")
    print('')

```

you don’t have to use ‘enumerate’ if you are not familiar. you can as easily use ‘for (var i = 0; i \< b.length; i++):’ . I also didn’t use any C array to save things and just printed out the result. if you need to use the result later, you need to save it somewhere.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:55pm UTC](https://forum.freecodecamp.org/t/index-of-duplicate-items-between-two-list-in-python/383122/10 "2021-06-01T18:55:46Z")

</div>

Closed on June 1, 2021, 6:55pm UTC


