This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: itertools: problem with nested groupby, list()
Type: Stage:
Components: None Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, nschloe
Priority: normal Keywords:

Created on 2010-05-04 09:47 by nschloe, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
iterator-test.py nschloe, 2010-05-04 09:47
Messages (3)
msg104917 - (view) Author: Nico Schlömer (nschloe) Date: 2010-05-04 09:47
Hi,

I ran into a bit of an unexpected issue here with itertools.

I need to say that I discovered itertools only recently, and that maybe my way of approaching the problem is "not what I want to do". If you think this may be the case, please let me know.

Anyway, the problem is the following:
I have a list of dictionaries, something like

[ { "a": 1, "b": 1, "c": 3 },
  { "a": 1, "b": 1, "c": 4 },
  ...
]

and I'd like to iterate through all items with, e.g., "a":1. What I do is sort and then groupby,

my_list.sort( key=operator.itemgetter('a') )
my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )

and then just very simply iterate over my_list_grouped,

for my_item in my_list_grouped:
    # do something with my_item[0], my_item[1]

Now, inside this loop I'd like to again iterate over all items with the same 'b'-value -- no problem, just do the above inside the loop:

for my_item in my_list_grouped:
        # group by keyword "b"
        my_list2 = list( my_item[1] )
        my_list2.sort( key=operator.itemgetter('b') )
        my_list_grouped = itertools.groupby( my_list2, operator.itemgetter('b') )
        for e in my_list_grouped:
            # do something with e[0], e[1]

That seems to work all right.

Now, the problem occurs when this all is wrapped into an outer loop, such as

for k in [ 'first pass', 'second pass' ]:
    for my_item in my_list_grouped:
    # bla, the above

To be able to iterate more than once through my_list_grouped, I have to convert it into a list first, so outside all loops, I go like

my_list.sort( key=operator.itemgetter('a') )
my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
my_list_grouped = list( my_list_grouped )

This, however, makes it impossible to do the inner sort and groupby-operation; you just get the very first element, and that's it.

An example file is attached.

Is there anything that I can do to debug?

Cheers,
Nico
msg104918 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-05-04 09:58
You'd be better off asking this on the python mailing list http://mail.python.org/mailman/listinfo/python-list (or in some other forum);  this tracker is for reporting bugs in Python itself, not bugs in code written in Python.

[The problem you're encountering here is that you can only iterate over the return of itertools.groupby once.  That's by design, though:  it's not a Python bug.]
msg104919 - (view) Author: Nico Schlömer (nschloe) Date: 2010-05-04 10:03
Okay, thanks for the hint.
Closing as invalid.
History
Date User Action Args
2022-04-11 14:57:00adminsetgithub: 52855
2010-05-04 10:03:43nschloesetmessages: + msg104919
2010-05-04 09:58:47mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg104918

resolution: not a bug
2010-05-04 09:47:21nschloecreate