0

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)
Tom
  • 79
  • 1
  • 10

3 Answers3

2
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'}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

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)
TBK
  • 466
  • 7
  • 22
0

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'}

Sanket Singh
  • 1,246
  • 1
  • 9
  • 26