Tom
Tom

Reputation: 79

How can I get the difference between two values in list

I'm trying to get the difference between temp1 and temp2 which will be 10.25.60.156 and 10.22.17.180 . Since the data in temp2 has brackets I've been receiving this error:

z = set(temp1).symmetric_difference(set(temp2))
TypeError: unhashable type: 'list'

. How can I get the difference between these two with one containing a bracket? Thanks in advance!

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

z = set(temp1).symmetric_difference(set(temp2))
print(z)

Upvotes: 0

Views: 69

Answers (3)

Sanket Singh
Sanket Singh

Reputation: 1366

Please check if this will work for you:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

temp2= [item for sublist in temp2 for item in sublist]
print(set(temp1).symmetric_difference(temp2))

Answer: {'10.22.17.180', '10.25.60.156'}

Upvotes: 0

TBK
TBK

Reputation: 466

Why do the elements of temp2 need to be lists? If they do, you could use list comprehension to select the 0th element of temp2 when comparing:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

z = set(temp1).symmetric_difference(set([x[0] for x in temp2))
print(z)

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195448

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

print( set(temp1).symmetric_difference(v[0] for v in temp2) )

Prints:

{'10.22.17.180', '10.25.60.156'}

Upvotes: 2

Related Questions