Tuple Comprehension ???

avi.e.gross at gmail.com avi.e.gross at gmail.com
Tue Feb 21 12:41:07 EST 2023


There is a very common misunderstanding by people learning python that a
tuple has something to do with parentheses. It confused me too at first.

A tuple is made by the use of one or more commas and no parentheses are
needed except when, like everything else, they are used for grouping as in
the arithmetic for 

  (5 + 4) * 3

So (6) is not a tuple while a trailing comma makes (6,) to be a tuple with
one entry.

A tad confusingly is that () by itself is a tuple, containing nothing. While
(,) is a syntax error!

A serious design issue in most computer languages is that there are too few
unique symbols to go around and some get re-used in multiple ways that
usually are not ambiguous when viewed in context. As an example, sets and
dictionaries both use curly braces but {} by itself is considered ambiguous
and they chose to make it be an empty dictionary. To get an empty set, use
set() instead. Parentheses are way overused and thus it gets murky at times
as when they are used to sort of make it clear you are using a generator.

Consider how this fails without parentheses:

result = x*2 for x in [1,2,3]
SyntaxError: invalid syntax

But with parentheses works fine:

result = (x*2 for x in [1,2,3])
result
<generator object <genexpr> at 0x0000029A3CFCF030>

However if you want a generator that is expanded into a list, you do not
need the parentheses duplicated like this:

result = list( (x*2 for x in [1,2,3]) )

and can just use this without nested parentheses:

result = list(  x*2 for x in [1,2,3]  )

For completeness, you arguably should have a concept of a comprehension for
every possible case but the people at python chose not to for reasons like
the above and especially as it is fairly simple to use this version:

result = tuple(  x*2 for x in [1,2,3]  )

Yes, it is a tad indirect and requires making a generator first.






-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Hen Hanna
Sent: Monday, February 20, 2023 11:14 PM
To: python-list at python.org
Subject: Re: Tuple Comprehension ???

On Monday, February 20, 2023 at 7:57:14 PM UTC-8, Michael Torrie wrote:
> On 2/20/23 20:36, Hen Hanna wrote: 
> > For a while, i've been curious about a [Tuple Comprehension]
> I've never heard of a "Tuple comprehension." No such thing exists as 
> far as I know.
> > So finally i tried it, and the result was a bit surprising... 
> > 
> > 
> > X= [ x for x in range(10) ]
> > X= ( x for x in range(10) )
> > print(X)
> > a= list(X)
> > print(a)


> What was surprising? Don't keep us in suspense! 
> 
> Using square brackets is a list comprehension. Using parenthesis 
> creates a generator expression. It is not a tuple.

ok!



    LisX= [x for x in range(10) ]

    print( sum( LisX ))
    print( max( LisX ))

    print( sum( x for x in range(10) ) )
    print( max( x for x in range(10) ) )

    print( * LisX )

    print( max( * LisX ))
    print( sum( LisX ))        # same as before
    # print( sum( * LisX )) <------- Bad syntax !!!

                       TypeError: sum() takes at most 2 arguments (10 given)


_____________________

                (A)   print( max( * LisX ))
                (B)   print( sum( * LisX ))        <------- Bad syntax !!!

What's most surprising is....     (A)  is ok, and  (B) is not.

           even tho'   max() and sum()  have   (basically)  the same
syntax...  ( takes one arg ,  whch is a list )



i've been programming for many years...        ( just knew to Python )
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list