built in list generator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • globalrev

    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))
  • Diez B. Roggisch

    #2
    Re: built in list generator?

    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

    Comment

    • Jerry Hill

      #3
      Re: built in list generator?

      On Tue, May 13, 2008 at 4:05 PM, globalrev <skanemupp@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

      Comment

      • afrobeard

        #4
        Re: built in list generator?

        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

        Comment

        • Ben Finney

          #5
          Re: built in list generator?

          "Diez B. Roggisch" <deets@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

          Comment

          • Andrii V. Mishkovskyi

            #6
            Re: built in list generator?

            2008/5/14 Ethan Furman <efurman@admail inc.com>:
            >
            Ben Finney wrote:
            >
            >
            Subject: Re: built in list generator?
            >
            From: Ben Finney <bignose+hate s-spam@benfinney. id.au>
            >
            Date: Wed, 14 May 2008 09:43:43 +1000
            >
            To: python-list@python.org
            >
            To: python-list@python.org
            >
            Newsgroups: comp.lang.pytho n
            >
            "Diez B. Roggisch" <deets@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.
            --



            --
            Wbr, Andrii Mishkovskyi.

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

            Comment

            Working...