I've been following this: check-if-value-already-exists-within-list-of-dictionaries.
However, I don't just want to check if the value already exists, I also want append
that value if it exists to my exists_list
and to not_exists_list
if it doesn't.
The problem is that I cannot "get" that value with the method I have been using because of the scope of the variable.
e.g
list_dict = [ { 'name': 'Lucas', 'addr': 4 }, { 'name': 'Gregor', 'addr': 44 }]
list_dict_comp = [ { 'name': 'Lucas', 'address': 'Lucas_XXASD' }, { 'name': 'Gregor', 'address': 'Gregor_ASDXX', { 'name': 'Iz', 'address': 'IZ_KOS' } }]
exists_list = []
not_exists_list = []
for i in list_dict:
if not any(a['name'] == i['name'] for a in list_dict_comp):
not_exists_list.append(a['address']) # <--- 'a' is not defined, because local scope.
else:
exists_list.append(a['address'])
ERROR
Traceback (most recent call last):
File "python3.py", line 11, in <module>
exists_list.append(a['address'])
NameError: name 'a' is not defined
EXPECTED OUTPUT
not_exists_list['IZ_KOS']
exists_list['Lucas_XXASD', 'Gregor_ASDXX']