Tuple Comprehension ???

Hen Hanna henhanna at gmail.com
Mon Feb 20 23:13:59 EST 2023


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 )


More information about the Python-list mailing list