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)
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.
Comments