How to create a tuple quickly with list comprehension?

F

fdu.xiaojf

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
 
M

markacy

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)
print a[0] 0
print a[9] 9
print a
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Cheers,
Marek
 
D

Diez B. Roggisch

markacy said:
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)
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
 
B

Ben Finney

I can use list comprehension to create list quickly. So I expected
that I can created tuple quickly with the same syntax.

Nope. The list comprehension syntax creates lists.
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))

This contains no commas, so I don't know why you think it has anything
to do with a tuple.

Bear in mind that parentheses have nothing to do with the syntax for
creating a tuple; the parentheses merely determine parsing order, and
can enclose any expression.
Is there a way to create a tuple like (1, 2, 3, 4, 5, 6, 7, 8, 9)
quickly?

tuple(range(1, 10))
 
M

Marc 'BlackJack' Rintsch

No need to create the intermediate list, a generator expression works just
fine:

a = tuple(i for i in range(10))

But `range()` creates the intermediate list anyway. ;-)

a = tuple(xrange(10))

Ciao,

Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top