3 ms·
Correct. There's no such thing as a python a-list, or p-list, and they can't share structure. There's overlap between programming in Python with lists, and prog
by samatman 3y ago
Correct. There's no such thing as a python a-list, or p-list, and they can't share structure. There's overlap between programming in Python with lists, and programming in Lisp with lists, but not as much as it appears.
In idiomatic Lisp, it's rather common to search a list for a value, cdr it, and cons that cdr to the collection you're building up. Python has nothing like that, because what I said makes no sense in terms of Python lists.
- o11c 3y agoWhat do a-list/p-list have to do with sharing structure? If you want shared structure of lists in Python, use `itertools.chain`. If you want key-value mappings, use a dict. Order is preserved by default nowadays, and conversion to/from an iterable of 2-tuples is trivial.
- thaumasiotes 3y ago> If you want shared structure of lists in Python, use `itertools.chain`. That would involve zero shared structure. itertools.chain is just an iterator that iterates one iterable and then, instead of declaring that it's done, goes on to iterate another one. Shared structure between lists means that two lists refer to some of the same regions of memory.
- o11c 3y agoAnd multiple `itertools.chain` instances pointing to the same list chunk(s) also use sharing.
- thaumasiotes 3y agoOK, that is true, but that approach would have the significant downside that you can't pass over a list more than once.
- d0mine 3y agoIterators can be shared in Python e.g., here's a well-known grouper recipe (traverse `it` iterator in chunks of `N` items each): zip(*[it]*N) Example: >>> L = [1, 2, 3, 4] >>> *zip(*[iter(L)]*2), ((1, 2), (3, 4)) Here's the single result of the iter() call is shared. The purpose is to show that iterator can be shared in principle (ignore other aspects of the code).