built in list generator?

G

globalrev

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))
 
D

Diez B. Roggisch

globalrev said:
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
 
J

Jerry Hill

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]
 
A

afrobeard

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top