473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to create a tuple quickly with list comprehension?

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
Jun 13 '07 #1
6 1989
On 13 Cze, 09:45, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
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)

Cheers,
Marek

Jun 13 '07 #2
markacy wrote:
On 13 Cze, 09:45, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
>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

Jun 13 '07 #3
"fd********@gmail.com" <fd********@gmail.comwrites:
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))

--
\ "Know what I hate most? Rhetorical questions." -- Henry N. Camp |
`\ |
_o__) |
Ben Finney
Jun 13 '07 #4
In <5d*************@mid.uni-berlin.de>, Diez B. Roggisch wrote:
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
Jun 13 '07 #5
fd********@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.
Jun 13 '07 #6
On Jun 13, 5:37 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <5d9ngkF32j8i...@mid.uni-berlin.de>, Diez B. Roggisch wrote:
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. ;-)
I imagine that's special case.

Jun 13 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lukas Kasprowicz | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Folks, My Proglem is, I get after a query on a mysql database with module MySQLdb a tuple but I need this output from database as a string....
5
by: Jay Davis | last post by:
I often need to multiply a tuple by a constant and the methods I use now are not very pretty. The idea is to get a new tuple that is some factor times the start tuple, like 'newtup = .5 *...
2
by: steph bagnis | last post by:
Hi ! I'm a new python programmer and I'm searching some tools for working with tuples's list. For example I have a list like that : and I would like to get a list with just the tuple with index...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
37
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
9
by: Odd-R. | last post by:
I have a dictionary, and I want to convert it to a tuple, that is, I want each key - value pair in the dictionary to be a tuple in a tuple. If this is the dictionary {1:'one',2:'two',3:'three'},...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
3
by: Mirco Wahab | last post by:
Hi, I have a 2D array, maybe irregular, like arr = , , ] if tried to pull an index list
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.