473,699 Members | 2,265 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 1246
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
3345
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 which speeds things up for larger lists but why was it necessary to add it instead of improving list...
23
2264
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
3464
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, chr(65+i)) for i in range(4)) > > Sure, but the same holds for list comprehensions: list(i*i for i in
4
1594
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 list to show only (say) primitive types or custom types or reference types? TIA
3
1254
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 not hard to write a function for this. But knowing Python is "battery included", I thought I'd...
14
3477
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 recursive function.
12
21132
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
1570
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 packet
12
13385
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
8706
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
8631
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9055
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...
1
8945
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6550
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
5889
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4392
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
4641
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.