Return the index of the common elements of two lists

Bryan Hii

So I have two lists:

The output only considers 1 and not 0

lst1 = [1,0,0,1,1]
lst2 = [0,0,1,1,0]

And the output is:

[3] # because the index where the elements are the same is at index 3

How should I code this?

The way I coded this was I tried having two loops

lst1 = [1,0,0,1,1]
lst2 = [0,0,1,1,0]
my_list = []

for i in range(len(lst1)):
    for i in range(len(lst2)):
        if lst1[i] == lst2[i] == 1:
            my_list.append(lst1[i])
        
print(my_list)
user1740577

try this:

lst1 = [1,0,0,1,1]
lst2 = [0,0,1,1,0]
[idx for idx, (f,s) in enumerate(zip(lst1,lst2)) if f==s]
#[1,3]

Edit: base on your comment. zip concatenate element by element of two list. see this example:

a = [1,2,4,6,8]
b = [0,3,5,7,9]
list(zip(a,b))
# [(1, 0), (2, 3), (4, 5), (6, 7), (8, 9)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Two common elements between lists

Delete common elements in two lists

common elements in two lists where elements are the same

Match elements between two lists but index for index

Find all common elements in two lists

finding common elements between two lists?

Common elements in two lists preserving duplicates

How to find the common elements of two lists?

Duplicates / common elements between two lists

checking if two lists have common elements

How detect common elements from two lists?

Elixir remove common elements from two lists

Scala - Merge two lists of tuples by common elements

Can't find common elements is two lists

Output Common Elements of Two Lists using Sets

Joining two lists with common elements - Scala

Common elements between two lists with no duplicates

find non common elements in two list of lists

Python join two lists with first index in common

Compare two lists to return a list with all elements as 0 except the ones which matched while keeping index?

Is there a way to compare two arrays and return a new array of the common elements which have the same index?

Groovy compare two lists to find lists with common elements

Return a list of elements that are NOT in two previous lists

How to return the count of the same elements in two lists?

Two lists of parameters to return a list of elements as tuples

Remove common elements in two lists and merge the two list [list]

Given two lists V1 and V2 of sizes n and m respectively. Return the list of elements common to both the lists and return the list in sorted order

get the index of the string elements which are common to more than one lists

Find common items between two lists with repeated elements python 3

TOP Ranking

HotTag

Archive