We can use defauldict to eliminate the use of an if condition when looking up keys
from collections import defaultdictusers = [{"username":"Jenna","language":"English"},{"username":"Michael","language":"French"},{"username":"Gustavo","language":"Spanish"},{"username":"Bhupesh","language":"English"},]# normal approachusers_by_language ={}for user in users:if user["language"]notin users_by_language: users_by_language[user["language"]]= [] users_by_language[user["language"]].append(user)print(users_by_language)# defaultdict approachusers_by_language =defaultdict(list)for user in users: users_by_language[user["language"]].append(user)print(dict(users_by_language))