markacy wrote: > On 13 Cze, 09:45, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >> 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? I already I can use tuple() on a list which is created by list >> comprehension to get a desired tuple. >> >> Regards, >> >> Xiao Jianfeng > > You should do it like this: > >>>> a = tuple([i for i in range(10)]) >>>> type(a) > <type 'tuple'> >>>> print a[0] > 0 >>>> print a[9] > 9 >>>> print a > (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
No need to create the intermediate list, a generator expression works just fine: a = tuple(i for i in range(10)) Diez -- http://mail.python.org/mailman/listinfo/python-list