Comparing two lists and making new list

Question:

So lets say I have two lists a=[1,2,3,4,5,6] and b=[2,34,5,67,5,6] I want to create a third list which will have 1 where elements are different in a and b and 0 when they are same, so above would be like c=[1,1,1,1,0,0]

Asked By: seric sheon

||

Answers:

Try a list comprehension over elements of each pair of items in the list with zip:

[ 0 if i == j else 1 for i,j in zip(a,b) ]
Answered By: Rich L

You can zip the lists and compare them in a list comprehension. This takes advantage of the fact that booleans are equivalent to 1 and 0 in python:

a=[1,2,3,4,5,6]
b=[2,34,5,67,5,6] 

[int(m!=n) for m, n, in zip(a, b)]
# [1, 1, 1, 1, 0, 0]
Answered By: Mark

Iterating with a for loop is an option, though list comprehension may be more efficient.

a=[1,2,3,4,5,6]
b=[2,34,5,67,5,6]
c=[]
for i in range(len(a)):
    if a[i] == b[i]:
        c.append(0)
    else:
        c.append(1)
print(c)

prints

[1, 1, 1, 1, 0, 0]
Answered By: Joe Thor

If you will have multiple vector operations and they should be fast. Checkout numpy.

import numpy as np
a=[1,2,3,4,5,6]
b=[2,34,5,67,5,6]
a = np.array(a)
b = np.array(b)
c = (a != b).astype(int)
# array([1, 1, 1, 1, 0, 0])
Answered By: samusa

idk if this is exactly what youre loocking for but this should work:

edidt: just found out that Joe Thor commented almost the exact same a few minutes earlier than me lmao

a = [1, 2, 3, 4, 5, 6]
b = [2, 34, 5, 67, 5, 6]
results = []

for f in range(0, len(a)):
    if a[f] == b[f]:
        results.append(0)
    else:
        results.append(1)

print(results)
Answered By: ghhiuu

This can be done fairly simply using a for loop. It does assume that both lists, a and b, are the same length. An example code would like something like this:

a = [1,2,3,4,5,6]
b = [2,34,5,67,5,6]
c = []
if len(a) == len(b):
   for i in range(0,len(a)):
       if(a[i] != b[i]):
         c.append(1)
       else:
         c.append(0)

This can also be done using list comprehension:

a = [1,2,3,4,5,6]
b = [2,34,5,67,5,6]
c = []
if len(a) == len(b):
    c = [int(i != j) for i,j in zip(a,b)]

The list comprehension code is from this thread: Comparing values in two lists in Python

Answered By: Pshivvy
a = [1, 2, 3, 4, 5, 6]
b = [2, 34, 5, 67, 5,6]
c = []

index = 0
x = 1
y = 0
for i in range(len(a)):    # iterating loop from index 0 till the last 
    if a[index]!= b[index]:    # comapring each index 
            c.append(x)         # if not equal append c with '1'
            index += 1           # increment index to move to next index in both lists
    else:
            c.append(y)
            index += 1

print(c)
Answered By: Muhammad Salman Ali

This should work for two lists of any type.

tstlist = ["w","s","u"]
lstseasons = ["s","u","a","w"]

lstbool_Seasons = [1 if ele in tstlist else 0 for ele in lstseasons]

Output: lstbool_Seasons = [1,1,0,1]

This is the first time I have posted anything, still figuring out how things work here, so please forgive faux pas…

Answered By: Borsheim
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.