Python Forum
alternative to nested loops for large data set
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
alternative to nested loops for large data set
#2
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'
Reply


Messages In This Thread
RE: alternative to nested loops for large data set - by Gribouillis - Feb-19-2020, 10:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Twilio alternative jmair 3 3,893 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Pillow alternative? kucingkembar 4 866 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  reduce nested for-loops Phaze90 11 1,867 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Read nested data from JSON - Getting an error marlonbown 5 1,358 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,706 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,572 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,215 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Break out of nested loops muzikman 11 3,326 Sep-18-2021, 12:59 PM
Last Post: muzikman
  How to break out of nested loops pace 11 5,358 Mar-03-2021, 06:25 PM
Last Post: pace

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020