[New-bugs-announce] [issue41030] Provide toList() method on iterators (`list()` is a flow killer in REPL)

Julien Edmond René Harbulot report at bugs.python.org
Fri Jun 19 04:41:27 EDT 2020


New submission from Julien Edmond René Harbulot <julien.harbulot at epfl.ch>:

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.

----------
components: Demos and Tools
messages: 371852
nosy: Julien Edmond René Harbulot
priority: normal
severity: normal
status: open
title: Provide toList() method on iterators (`list()` is a flow killer in REPL)
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41030>
_______________________________________


More information about the New-bugs-announce mailing list