I want to get the elements in a list that match by substring while iterating over it.
For instance, in following example list, i want to get "pass1", "pass2" matching elements and "pass3" as element that doesn't match. This is a simplified version of the list, as I need to iterate over the long list.
Please, let me know, what am I doing wrong in the following example.
keyList=["pass1_v1","pass1_v3","pass1_v5","pass2_v1","pass2_v3","pass3_v4"]
for x in keyList:
match=x.rsplit("_",1)[0] ## splitting the list elements seperated by "_"..eg:- pass1 to check how many elements match
if match in keyList:
print("matching are %s" %x) ## expecting to print eg:-pass1_v1 and pass1_v3
else :
print ("non matching are %s"%x) # expecting to print pass3_v4
pass3is also a substring in the list (the last element). Do you mean that it has to match other elements in the list?pass1in the first loop, in which case all thepass2andpass3elements would not match that prefix. Next two iterations the same would happen. Then apass2prefix is found, and it appears you want allpass1andpass3elements to be printed as 'not matching'; twice as there are twopass2elements in the list. That's a lot of extranon matchingprints..