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: Provide toList() method on iterators/generators (`list()` is a flow killer in REPL)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Julien H, ammar2, remi.lapeyre, zach.ware
Priority: normal Keywords:

Created on 2020-06-19 08:41 by Julien H, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg371852 - (view) Author: Julien H (Julien H) Date: 2020-06-19 08:41
I work with python in the REPL or jupyter notebooks for my data science work and often find myself needing to explore data structures or directories on disk.

So I'll access these data structure in a linear "go-forward" fashion using the dot, for instance:

```
root_directory = pathlib.Path(...)
root_directory.iterdir()[0].iterdir()
```

Which of course doesn't work because iterdir() provides an iterator instead of a list (I understand this is good for performance, but how annoying for interactive sessions!)

The problem with the current API is that:

1. I have to go back to my code and edit in several places to convert the iterators to lists. With the currrent way (i.e. `list( ... )`) I have to edit before the iterator to add `list(` and then after to add `)`. When my one-liners become complex, this is very tedious because I have to perform extensive search to find where to insert the `list(` part. Having a method `.toList()` would allow to edit the expression in a *single* place instead, and is also much easier to read in my opinion:

```
root_directory.iterdir().toList()[0].iterdir().toList()
```

instead of:

```
list(list(root_directory.iterdir())[0].iterdir())
```

2. I want to think about my work, the business task at hand. Not about the particularities of python iterators. Having to specify `list(` first, forces me to think about the language. This gets in my way. The possiblity to use `.toList()` at the end of an expression allows the conversion to happen as an afterthought. In particular in REPL or notebooks where I'll often write:

```
directory.iterdir()
```

And the repl will display 

```
<generator object Path.iterdir at 0x7ff873249660>
```

How easy would it be if I could just *append* to my code instead of surgically edit before and after.
msg371854 - (view) Author: Julien H (Julien H) Date: 2020-06-19 08:45
Consider how easy in the REPL it is to hit `up arrow` and then append code to the previously entered command, compared to having to move the cursor back in the command to edit it somewhere in the middle.

Simulated REPL session:

```
dir.iterdir()
> <generator object Path.iterdir at 0x7ff873249660>

<up arrow> .toList()
> [Path(...), Path(...)]
```
msg371856 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2020-06-19 08:54
Julien, in the REPL "_" represents the last evaluated expression. So you can do:

>>> iter(range(10))
<range_iterator object at 0x03436578>
>>> list(_)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Do you feel like that covers what you're asking for?
msg371857 - (view) Author: Julien H (Julien H) Date: 2020-06-19 08:59
Hello Ammar Askar,

I agree `_` avoids the "up arrow" problem I mentioned in the REPL. 

I actually primarily use jupyter notebooks in my work.

Point 1. in my first message is the primary issue. Having to edit the line in two places to perform one action has driven me crazy over and over.
msg371858 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-19 09:20
It would not work, an iterator is just a thing that has __next__() and __iter__():


class MyIterator:
    def __iter__(self): return self
    def __next__(self): return 1


Plenty of iterators would not have this method even if we added it to some of them in the standard library and you would be event more frustated that sometimes toList(), and sometimes doesn't.



If you need to move in the current line in the REPL you can use Ctrl-a and Ctrl-e and you can use Ctrl + arrow in a Jupyter Notebook.
msg371889 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-06-19 15:37
Hi Julien,

Thanks for the suggestion, but I agree with Ammar and Remi that this is not likely to either be easily doable or actually help your case.  This would also be a big enough change to likely need a PEP.

If you still want to pursue this I would suggest starting a discussion on the python-ideas@python.org mailing list, but for now I'm going to go ahead and close this issue.
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85202
2020-06-19 15:37:58zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg371889

resolution: rejected
stage: resolved
2020-06-19 09:20:42remi.lapeyresetnosy: + remi.lapeyre
messages: + msg371858
2020-06-19 08:59:52Julien Hsetmessages: + msg371857
2020-06-19 08:54:52ammar2setnosy: + ammar2
messages: + msg371856
2020-06-19 08:54:21Julien Hsettitle: Provide toList() method on iterators (`list()` is a flow killer in REPL) -> Provide toList() method on iterators/generators (`list()` is a flow killer in REPL)
2020-06-19 08:52:29Julien Hsetcomponents: + Library (Lib), - Demos and Tools
versions: - Python 3.9
2020-06-19 08:45:24Julien Hsetmessages: + msg371854
2020-06-19 08:41:27Julien Hcreate