I'd like to know how to compare and delete the values of the two lists at the same location.

Asked 2 years ago, Updated 2 years ago, 12 views

a=[1,2,3,4,5,6,7] b=[0,2,8,9,10,11,12] a[0] to the end of the element If a[1]==b[1], I want to print out the list a and b that deleted a[1] and b[1].

If you do it like below, the out of range error comes out. I think it's because the number of elements in list a is decreasing.

Thank you.

a=[1,2,3,4,5,6,7]
b=[0,2,8,9,10,11,12]
for i in range(0,len(a))
    if a[i]==b[i]:
        del a[i]
        del b[i]

python

2022-09-21 11:09

1 Answers

a = [1,2,3,4,5,6,7]
b = [0,2,8,9,10,11,12]
new_a = []
new_b = []
for a_i, b_i in zip(a, b):
    if a_i != b_i:
        new_a.append(a_i)
        new_b.append(b_i)


2022-09-21 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.