I have a list called animals,
animals = ["B_FOX", "A_CAT", "A_DOG", "A_MOUSE",
"B_DOG", "B_MOUSE", "C_DUCK", "C_FOX", "C_BIRD"]
and would like the following outputs:
A = ["A_CAT", "A_DOG", "A_MOUSE"]
B = ["B_DOG", "B_MOUSE", "B_FOX"]
C = ["C_DUCK", "C_FOX", "C_BIRD"]
I can only get a subset list of only the letters or the animals like this:
[species.split("_",1)[1] for species in animals]
['FOX', 'CAT', 'DOG', 'MOUSE', 'DOG', 'MOUSE', 'DUCK', 'FOX', 'BIRD']
[letters.split("_",1)[0] for letters in animals]
['B', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'C']
Not sure if I've worded the question correctly. Any help in solving this tricky problem would be greatly appreciated!