473,406 Members | 2,954 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,406 software developers and data experts.

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,100, 2))
Jun 27 '08 #1
5 1214
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,100, 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*******@yahoo.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,100, 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.web.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,100, 2))

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

Diez
Jun 27 '08 #4
"Diez B. Roggisch" <de***@nospam.web.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,100, 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*****@admailinc.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*********@python.org

To: py*********@python.org

Newsgroups: comp.lang.python

"Diez B. Roggisch" <de***@nospam.web.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,100, 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
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...
23
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...
30
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,...
4
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...
3
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...
14
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...
12
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...
12
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...
12
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.