How to create a tuple quickly with list comprehension?
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Jun 13 06:43:06 EDT 2007
fdu.xiaojf at gmail.com a écrit :
> Hi all,
>
> I can use list comprehension to create list quickly. So I expected that I
> can created tuple quickly with the same syntax. But I found that the
> same syntax will get a generator, not a tuple. Here is my example:
>
> In [147]: a = (i for i in range(10))
>
> In [148]: b = [i for i in range(10)]
>
>
> In [149]: type(a)
> Out[149]: <type 'generator'>
>
> In [150]: type(b)
> Out[150]: <type 'list'>
>
> Is there a way to create a tuple like (1, 2, 3, 4, 5, 6, 7, 8, 9)
> quickly?
t = tuple(range(1, 10))
If the use of 'range' in your above snippet was just for the exemple,
see Diez's answer.
More information about the Python-list
mailing list