Can I turn a generator object into a tuple without using “tuple()”? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can I turn a generator object into a tuple without using “tuple()”? [duplicate]



This question already has an answer here:



It's possible to use the following code to create a list:


>>> [i+1 for i in(0,1,2)]
[1, 2, 3]



Can a similar thing be done with tuples?


>>> (i+1 for i in(0,1,2)),
(<generator object <genexpr> at 0x03A53CF0>,)



I would have expected (1, 2, 3) as the output.


(1, 2, 3)



I know you can do tuple(i+1 for i in(0,1,2)), but since you can do [i+1 for i in(0,1,2)], I would expect a similar thing to be possible with tuples.


tuple(i+1 for i in(0,1,2))


[i+1 for i in(0,1,2)]



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





No, there are no tuple comprehensions.
– L3viathan
Aug 12 at 17:12





No; parentheses actually aren't the syntax for a tuple, it's the comma. There's no "tuple comprehension", see e.g. stackoverflow.com/q/16940293/3001761.
– jonrsharpe
Aug 12 at 17:12






@jonrsharpe I know. >>> 1, outputs (1,). I would still expect there to be a way to do that though.
– Super S
Aug 12 at 17:30


>>> 1,


(1,)




1 Answer
1



In python 3 you can unpack a generator using *.


*



Here is an example:


>>> *(i+1 for i in (1,2,3)),
(2, 3, 4)

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered