I have a list like this (suppose it’s sorted key=lambda x: x['string_id']
):
list1 = [
{'string_id': 'CAB', 'number': 1},
{'string_id': 'CAB', 'number': 1},
{'string_id': 'CAB', 'number': 1},
{'string_id': 'CAB', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'ACB', 'number': 1},
{'string_id': 'ACB', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
]
I’m going to create a new list but only to keep, let’s say 2 of each string_id
.
I mean something like this:
list2 [
{'string_id': 'CAB', 'number': 1},
{'string_id': 'CAB', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'BAC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
{'string_id': 'ABC', 'number': 1},
]
How can I do this?
1