I want to check if a dictionary already exists in a group of lists. Without the use of any() in the if-statement, I received the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
But then when I do make use of the any(), the following error arises:
TypeError: 'bool' object is not iterable
I don't know where I'm going wrong. Any help or guidance on this would be much appreciated.
import numpy as np
compile = []
MY_LIST = [[['Mon'], np.array([4,2,1,3]), ['text'], ['more_text', 0.1]], [['Mon'], np.array([4,2,1,3]), ['text'], ['more_text', 0.1]], [['Tues'], np.array([3, 1, 2, 4]), ['text2'], ['more_text2', 0.2]]]
for group in MY_LIST:
my_dict = {'A': group[0], 'B': group[1], 'C': group[2],
'D': group[3]}
if any(my_dict not in compile): # Error here
compile.append(my_dict)
print (compile)
The expected output should be:
[{'A': ['Mon'], 'B': array([4, 2, 1, 3]), 'C': ['text'], 'D': ['more_text', 0.1]}, {'A': ['Tues'], 'B': array([3, 1, 2, 4]), 'C': ['text2'], 'D': ['more_text2', 0.2]}]