I have a variable (x) which has multiple lists under it. Last item of the list is referencing the first item in the lists. (e.g list which has a last item value of ‘1’ is compatible with a list where its first item value is ‘1’)
['1', 'test', '3']
['2', 'test2', '1']
['3', 'test3', '4']
['4', 'test4', '3']
How could I first search for lists that have test2 in them, then search a list which is compatible with it and then delete every other list which isn’t neither of these two.
Result should be the following:
['1', 'test', '3']
['2', 'test2', '1']
Haven’t been able to figure the answer out at all. Also a place of concern is that ‘test2’ compatible list could be above it or under it (like it is in the case with ‘test3 list).
>Solution :
Do it in two steps:
- collect all the triples that have ‘test2’ in them;
- collect all the triplets that are compatible.
x = [
['1', 'test', '3'],
['2', 'test2', '1'],
['3', 'test3', '4'],
['4', 'test4', '3']
]
have_test2 = [t for t in x if t[1] == 'test2']
compatible_numbers = {n for (_,_,n) in have_test2}
are_compatible = [t for t in x if t[0] in compatible_numbers]
print(have_test2, are_compatible)
# [['2', 'test2', '1']] [['1', 'test', '3']]
Or slightly more direct:
x = ...
compatible_numbers = {n for (_,test,n) in x if test == 'test2'}
all_results = [t for t in x if t[1] == 'test2' or t[0] in compatible_numbers]
print(all_results)
# [['1', 'test', '3'], ['2', 'test2', '1']]
Important note
You might notice that I used curly brackets { } when defining compatible_numbers. This notation defines a set rather than a list. set are a slightly different data structure than lists, and they are particularly efficient when there is a need to test membership. The membership test if t[0] in compatible_numbers is fast because compatible_numbers is a set; it would be slow if compatible_numbers was a list.
Note that set here is the name of a python data structure. It is a relatively standard name for similar structures in other programming languages. This data structure shares its name with the mathematical objects "set". This shared name is not a coincidence, but still, programming sets and mathematical sets are not the same thing 😉