def remove_after_kth(items, k=1): Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded. Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k. Note the counterintuitive and yet completely legitimate edge case of k==0 that has the well defined and unambiguously correct answer of an empty list! Once again, an often missed and yet so very important part of becoming a programmer is learning to perceive zero as a number.
That's enough of you!
def remove_after_kth(items, k=1):
Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded.
Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k.
Note the counterintuitive and yet completely legitimate edge case of k==0 that has the well defined and unambiguously correct answer of an empty list! Once again, an often missed and yet so very important part of becoming a programmer is learning to perceive zero as a number...
![items
k
Expected result
[42, 42, 42, 42, 42, 42, 42]
3
[42, 42, 42]
'bob', 'bob',
['tom', 42,
'bob', 'tom', 'tom', 99]
99,
2
['tom', 42, 'bob',
'bob', 99, 'tom', 99]
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2,
3, 4, 5, 4, 3, 2, 1]
1
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2,
[1, 2, 3, 4, 5, 4, 3,
2, 1, 2, 3, 4, 5, 1, 5]
3
3, 4, 5, 4,
3, 2, 1, 2, 3,
4, 5]
[42, 42, 42, 99, 99, 17]](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fb9ec6203-7441-453e-8993-3e6380986f00%2F6fcb7d2b-97f3-4d2c-846f-cd99c725ebea%2Fvacfk0f_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)