classification
Title: composing generator expression doesn't work as expected
Type: behavior Stage: resolved
Components: Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder: nested generator expression produces strange results
View: 7423
Assigned To: Nosy List: SilentGhost, Tsvika Shapira
Priority: normal Keywords:

Created on 2019-04-14 09:32 by Tsvika Shapira, last changed 2019-04-14 10:15 by SilentGhost. This issue is now closed.

Messages (2)
msg340200 - (view) Author: Tsvika Shapira (Tsvika Shapira) Date: 2019-04-14 09:32
the following code:

```
lists_to_filter = [
    ['a', 'exclude'],
    ['b']
]
# notice that when 'exclude' is the last element, the code returns the expected result
for exclude_label in ['exclude', 'something']:
    lists_to_filter = (labels_list for labels_list in lists_to_filter if exclude_label not in labels_list)
    # notice that changing the line above to the commented line below (i.e. expanding the generator to a list) will make the code output the expected result, 
    # i.e. the issue is only when using filter on another filter, and not on a list
    # lists_to_filter = [labels_list for labels_list in lists_to_filter if exclude_label not in labels_list]
lists_to_filter = list(lists_to_filter)
print(lists_to_filter)
```

as far as i understand, the code above should output "[['b']]"
instead it outputs "[['a', 'exclude'], ['b']]"
msg340203 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-04-14 10:15
I probably won't be able to better explain the issue then Benjamin did in the referenced issue. Just to note, that (...) in your code are called generator expressions.
History
Date User Action Args
2019-04-14 10:15:16SilentGhostsetstatus: open -> closed

superseder: nested generator expression produces strange results
title: composing filter() doesn't work as expected -> composing generator expression doesn't work as expected
nosy: + SilentGhost

messages: + msg340203
resolution: not a bug
stage: resolved
2019-04-14 09:32:03Tsvika Shapiracreate