Feb-19-2020, 10:40 PM
(This post was last modified: Feb-19-2020, 10:40 PM by Gribouillis.)
You could start by grouping the card sections by their card_id. There are several ways to do that, here is one
from operator import itemgetter import itertools dic = {int(key): list(group) for key, group in itertools.groupby( sorted(card_sections, key=itemgetter('card_id')), key=itemgetter('card_id')))} card_names = [] for card_id, qty in owned_cards.items(): for sections in dic.get(card_id, []): card_names.extend('{}#{}'.format(s['name'], qty) for s in sections)Alternately you could use
more_itertools.bucket()
if you have this module. Also note that in Python, 20 != '20'