473,560 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

built in list generator?

if i want a list with all numbers between x and y is there a way to
do this with an inbult function.

i mean i can always construct a function to do this but is there
soemthing like:

nbrs = list(range(50,1 00, 2))
Jun 27 '08 #1
5 1228
globalrev schrieb:
if i want a list with all numbers between x and y is there a way to
do this with an inbult function.

i mean i can always construct a function to do this but is there
soemthing like:

nbrs = list(range(50,1 00, 2))
range *does* that. use xrange if all you want is to iterate.

Diez
Jun 27 '08 #2
On Tue, May 13, 2008 at 4:05 PM, globalrev <sk*******@yaho o.sewrote:
if i want a list with all numbers between x and y is there a way to
do this with an inbult function.

i mean i can always construct a function to do this but is there
soemthing like:

nbrs = list(range(50,1 00, 2))
What's wrong with just using range()?
>>range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>range(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>range(1, 11, 2)
[1, 3, 5, 7, 9]
>>>
--
Jerry
Jun 27 '08 #3
range(50,100,2) returns a list of numbers starting from 50 and less
than 100 with a step size of 2.

list() takes any iterable datatype and converts it into a list.

e.g. list((1, 2, 3)) would return [1,2,3]
& list([1, 2]) would return [1,2]

In this case there is no point of calling range within list since the
output of range is already a list.

Note list(xrange(50, 100,2)) would have made sense if range didnt exist
and you needed a list, but since range does exist, I dont see the
point.

Diez is right when he says to use list to iterate, because creating a
big list just for the sake of iteration would be a terrible waste of
space.

On May 14, 1:16*am, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
globalrev schrieb:
if i want *a list with all numbers between x and y is there a way to
do this with an inbult function.
i mean i can always construct a function to do this but is there
soemthing like:
nbrs = list(range(50,1 00, 2))

range *does* that. use xrange if all you want is to iterate.

Diez
Jun 27 '08 #4
"Diez B. Roggisch" <de***@nospam.w eb.dewrites:
globalrev schrieb:
if i want a list with all numbers between x and y is there a way to
do this with an inbult function.

i mean i can always construct a function to do this but is there
soemthing like:

nbrs = list(range(50,1 00, 2))

range *does* that. use xrange if all you want is to iterate.
Until Python 3.0, where 'range' returns an iterable.

--
\ "It seems intuitively obvious to me, which means that it might |
`\ be wrong." -- Chris Torek |
_o__) |
Ben Finney
Jun 27 '08 #5
2008/5/14 Ethan Furman <ef*****@admail inc.com>:
>
Ben Finney wrote:
Subject: Re: built in list generator?

From: Ben Finney <bi************ ****@benfinney. id.au>

Date: Wed, 14 May 2008 09:43:43 +1000

To: py*********@pyt hon.org

To: py*********@pyt hon.org

Newsgroups: comp.lang.pytho n

"Diez B. Roggisch" <de***@nospam.w eb.dewrites:

globalrev schrieb:
if i want a list with all numbers between x and y is there a way to
do this with an inbult function.

i mean i can always construct a function to do this but is there
soemthing like:

nbrs = list(range(50,1 00, 2))

range *does* that. use xrange if all you want is to iterate.

Until Python 3.0, where 'range' returns an iterable.

Is there a thread somewhere of the discussion for this change? I'm
presuming range is used almost exclusively in loops where a change in the
return type would have no negative effect.
--
http://mail.python.org/pipermail/pyt...il/034534.html
Ethan

--
http://mail.python.org/mailman/listinfo/python-list


--
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
Jun 27 '08 #6

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

Similar topics

24
3330
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation...
23
2233
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit confused, though - when would I use a list comp instead of a generator expression if I'm going to require 2.4 anyway? Thanks, <mike --
30
3447
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course, is that dict comprehensions don't gain you >> much at all over the dict() constructor plus a generator expression, >> e.g.: >> dict((i,...
4
1589
by: CrispinH | last post by:
I'm building an application generator and within it a user can create a new property (for the class they are building). I'd like then to offer a list of Types - built-in and custom - for this new property to be shown in a combo box ie similar to Intellisense. How do you enumerate the Types available? Are their mechanisms for filtering the...
3
1250
by: wcc | last post by:
Hello, Beginner learning Python here. I know filter(lambda x: x > 3, ) will give me a list . What if I only need to find the first item in the list that returns Ture when applying the filter function. In this case, I only want to get the 5, and its index 2. Is there a built-in function, or function from a module for that? I think it is...
14
3461
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind of parent/child relationship. Now I would like to get all children of a given "Device" object and thought that it would be the best way to use...
12
20955
by: Dave Dean | last post by:
Hi all, I'm looking for a way to iterate through a list, two (or more) items at a time. Basically... myList = I'd like to be able to pull out two items at a time - simple examples would be: Create this output: 1 2
12
1564
by: danmcleran | last post by:
All, I can't seem to find an answer to this question anywhere, but I'm still looking. My problem is I have a list of values like this: l = A value with bit 0x80 set delineates the start of a new packet of information. What I want to do is to group the packets so that 1, 2, 3 go with the 1st packet tagged 0xF0, 4 ,5, 6 go with the 2nd...
12
13367
by: Samir | last post by:
Hi Everyone, I am relatively new to Python so please forgive me for what seems like a basic question. Assume that I have a list, a, composed of nested lists with string representations of integers, such that a = , , , ]
0
7635
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...
0
7556
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8068
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...
0
6193
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...
0
5176
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3607
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...
0
3589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1171
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.