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: List amnesia
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: The Compiler, christian.heimes, marciomocellin
Priority: normal Keywords:

Created on 2021-03-09 19:02 by marciomocellin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg388372 - (view) Author: Márcio Mocellin (marciomocellin) Date: 2021-03-09 19:02
In Python 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux, when I materialize the list, it is shown and is deleted. Shouldn't the list persist in memory? Is this a bug or is it really?

```python
>>> vet_neg
['EH01', 'EH02', 'EH03']
Categories (3, object): ['EH01', 'EH02', 'EH03']
>>> cenarios
[0, 1]
>>> vet_neg_cenarios = itertools.product(vet_neg, cenarios, cenarios)
>>> vet_neg_cenarios
<itertools.product object at 0x7f6fa16a65e8>
>>> list(vet_neg_cenarios)
[('EH01', 0, 0), ('EH01', 0, 1), ('EH01', 1, 0), ('EH01', 1, 1), ('EH02', 0, 0), ('EH02', 0, 1), ('EH02', 1, 0), ('EH02', 1, 1), ('EH03', 0, 0), ('EH03', 0, 1), ('EH03', 1, 0), ('EH03', 1, 1)]
>>> list(vet_neg_cenarios)
[]
>>> 
```
msg388375 - (view) Author: Florian Bruhin (The Compiler) * Date: 2021-03-09 19:17
This is not a bug. itertools.product returns an iterator: https://docs.python.org/3/glossary.html#term-iterator

Quoting from there:

> [...] every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
msg388376 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-03-09 19:20
Florian's answer is correct. Thanks!
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87616
2021-03-09 19:20:31christian.heimessetstatus: open -> closed

type: behavior

nosy: + christian.heimes
messages: + msg388376
resolution: not a bug
stage: resolved
2021-03-09 19:17:16The Compilersetnosy: + The Compiler
messages: + msg388375
2021-03-09 19:02:51marciomocellincreate