[issue39784] Tuple comprehension

Marco Sulla report at bugs.python.org
Fri Feb 28 12:44:14 EST 2020


New submission from Marco Sulla <launchpad.net at marco.sulla.e4ward.com>:

I think a tuple comprehension could be very useful.

Currently, the only way to efficiently create a tuple from a comprehension is to create a list comprehension (generator comprehensions are more slow) and convert it with `tuple()`.

A tuple comprehension will do exactly the same thing, but without the creation of the intermediate list.

IMHO a tuple comprehension can be very useful, because:

1. there are many cases in which you create a list with a comprehension, but you'll never change it later. You could simply convert it with `tuple()`, but it will require more time
2. tuples uses less memory than lists
3. tuples can be interned

As syntax, I propose 

(* expr for x in iterable *)

with absolutely no blank character between the character ( and the *, and the same for ).

Well, I know, it's a bit strange syntax... but () are already taken by generator comprehensions. Furthermore, the * remembers a snowflake, and tuples are a sort of "frozenlists".

----------
components: Interpreter Core
messages: 362888
nosy: Marco Sulla
priority: normal
severity: normal
status: open
title: Tuple comprehension
versions: Python 3.9

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


More information about the Python-bugs-list mailing list