473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2006
On 13 Cze, 09:45, "fdu.xia...@gma il.com" <fdu.xia...@gma il.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...@gma il.com" <fdu.xia...@gma il.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
comprehensio n 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********@gma il.com" <fd********@gma il.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********@gmai l.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.net wrote:
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
18202
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. can anybody help? - -------------- database ---------
5
16767
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 * oldtup'. Is there a slick way to do this?
2
1808
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 0 '+' or things like that. The question is : is it possible without doing a loop ? Thx
50
3690
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, I'm just curious why it is neccesary.. Thanks,
37
2391
by: Gregor Horvath | last post by:
Hi, >>>type() <type 'list'> >>>type(('1')) <type 'str'> I wonder why ('1') is no tuple????
9
19208
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'}, then I want this to be the resulting tuple: ((1,'one'),(2,'two'),(3,'three')). I have been trying for quite some time now, but I do not get the result as I want it. Can this be done, or is it not
18
2720
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 http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html In this post, I'm especially soliciting review of Carl Banks's point (now discussed under Open Issues) which asks if it would be better to have the create statement translated into:
3
2085
by: Mirco Wahab | last post by:
Hi, I have a 2D array, maybe irregular, like arr = , , ] if tried to pull an index list
0
1792
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 reasonable since generators require iteration and stop iteration and what not.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.