Python: Better way to split list of object by a field?
list, python
Solution
You can use `itertools.groupby` (https://docs.python.org/3.6/library/itertools.html#itertools.groupby) to achieve what you want:
from itertools import groupby
grouped_data = groupby(persons_data, key=lambda x: x[1]) # or x.country, depending on your input list
for country, items in grouped_data:
# do whatever you want
There are a few gotchas to keep in mind:
- `groupby` returns an iterator, so you can only iterate over it once.
- the `items` in my example above is an iterator, too. So you'll need to cast it to a list if you want to access the individual items by index later.
Problem
I have a list of object named person with id and their countries: ``` class Person(object): def __init__(self, id, country): self.id = str(id) self.country = str(country) ``` The list looks like the below, where id is just UUID and country being country codes, I have sorted them by country: ``` ('7e569521-69fe-4ccf-a898-254bd758bff0', 'AF') ('c6b45478-6901-4a22-aab8-7167397d4b13', 'AF') ('15aee743-a1b1-4a77-b93b-17786c8c8fab', 'AF') ('7ef1efd3-6b77-4dfe-b133-035eff76d7f6', 'AF') ('95880e05-9984-48e3-a60a-0cf52c2915ae', 'AG') ('620862a0-e888-4b20-8057-085122226050', 'AL') ('ed0caf58-e132-48ad-bfca-8a4df2b0c351', 'AL') ('730cf6ba-0981-4a0b-878e-5df0ebedaa99', 'AM') ('93f87a3d-d618-4e9a-9f44-4a1d0bc65bdc', 'AM') ``` Now I would like to split them into different lists by country. This is what I am doing now: ``` prev_country = "" person_data_country = [] for person in persons_data: if prev_country != person.country: if len(person_data_country) > 0: # do something with this new list by country # clear them person_data_country = [] # append item to new list person_data_country.append(person) prev_country = person.country # last list, if any if len(person_data_country) > 0: # do something with this new list by country ``` I get what I want with the above codes. But I would like to know if there is a better or more efficient way to split the list according to country?