Answer the question
In order to leave comments, you need to log in
How to remove elements from a list with the same ending?
Let's say we have a list:
l1 = ['Ivanov', 'Petrov', 'Sidorov', 'Pears', 'Apples', 'Table', 'Car', 'Lamp', '940345']
Answer the question
In order to leave comments, you need to log in
To paraphrase - you need to remove from the list all elements for which there is at least one other element with matching N last characters. So?
Use collections.Counter.
l1 = ['Ивановы', 'Петровы', 'Сидоровы', 'Груши', 'Яблоки', 'Стол', 'Машина', 'Лампа', '940345']
N = 2
suffixes = collections.Counter( item[-N:] for item in l1 )
l2 = list( filter( lambda item: suffixes[item[-N:]] <= 1, l1 ) )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question