list of tuple

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

    list of tuple

    Hi !
    I'm a new python programmer and I'm searching some tools for working
    with tuples's list.
    For example I have a list like that :
    [('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)]
    and I would like to get a list with just the tuple with index 0 '+' or
    things like that. The question is : is it possible without doing a
    loop ?

    Thx
  • Peter Hansen

    #2
    Re: list of tuple

    steph bagnis wrote:
    [color=blue]
    > I'm a new python programmer and I'm searching some tools for working
    > with tuples's list.
    > For example I have a list like that :
    > [('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)]
    > and I would like to get a list with just the tuple with index 0 '+' or
    > things like that. The question is : is it possible without doing a
    > loop ?[/color]

    I guess it depends on what you consider a loop to be:

    For example, a list comprehension should do the trick:

    newlist = [x[0] for x in oldlist]

    (This is equivalent to a for loop that goes through each item
    in the old list, appending the 0-th element of each tuple to a new
    list as it goes.)

    -Peter

    Comment

    • Russell Blau

      #3
      Re: list of tuple

      "Peter Hansen" <peter@engcorp. com> wrote in message
      news:TJKdnUm2dc Exb1vdRVn-gQ@powergate.ca ...[color=blue]
      > steph bagnis wrote:
      >[color=green]
      > > I'm a new python programmer and I'm searching some tools for working
      > > with tuples's list.
      > > For example I have a list like that :
      > > [('+',4,3),('+', 3,9),'('+',5,6) ,('x',10),('x', 3)]
      > > and I would like to get a list with just the tuple with index 0 '+' or
      > > things like that. The question is : is it possible without doing a
      > > loop ?[/color]
      >
      > I guess it depends on what you consider a loop to be:
      >
      > For example, a list comprehension should do the trick:
      >
      > newlist = [x[0] for x in oldlist]
      >
      > (This is equivalent to a for loop that goes through each item
      > in the old list, appending the 0-th element of each tuple to a new
      > list as it goes.)[/color]

      If I understood the OP correctly, he wants to select those tuples that have
      a '+' as the first item. So maybe he wants:

      newlist = [x for x in oldlist if x[0] == '+']

      --
      I don't actually read my hotmail account, but you can replace hotmail with
      excite if you really want to reach me.


      Comment

      Working...