I have a nested list
test_list = [['AA', 'OFF'], ['BB', 'OFF'], ['BB', 'ON'], ['BB', 'ON'], ['CC', 'OFF'], ['DD', 'OFF'], ['EE', 'OFF']]
I would like to find matching index 0 sub-elements such as
['BB', 'OFF'], ['BB', 'ON'], ['BB', 'ON']
and filter out any sub-items with 'OFF' in index 1 as long as there is a matching index 0 with ' ON' found and not keep duplicates, otherwise just keep the sub-elements.
So for the above example, the end result would be:
[['AA', 'OFF'], ['BB', 'ON'], ['CC', 'OFF'], ['DD', 'OFF'], ['EE', 'OFF']]
I tried converting the above to a dictionary but the result changes depending on the chronological order of the list:
test_dict = dict(test_list)
test_dict
output:
{'AA': 'OFF', 'BB': 'ON', 'CC': 'OFF', 'DD': 'OFF', 'EE': 'OFF'}
Which is exactly what I want
BUT for
test_list1 = [['AA', 'OFF'],['BB', 'ON'],['BB', 'OFF'],['CC', 'OFF'],['DD', 'OFF'],['EE', 'OFF']]
test_dict1 = dict(test_list1)
test_dict1
{'AA': 'OFF', 'BB': 'OFF', 'CC': 'OFF', 'DD': 'OFF', 'EE': 'OFF'}
Which neglects ['BB', 'ON'] because of its position and is not fitting to what I want.
The order of the test_table list doesn't really matter as long as the end values are unique and meet the 'ON' / 'OFF' criteria above.
I guess I am looking for a way to group and sort the nested list so that I can then turn it into a dictionary and output the desired result?
['BB', 'OFF'], ['BB', 'OFF'], ['BB', 'ON'],, would you wantONorOFF?ONis in the list then it has to be the one displayed andOFFshould be dropped.