list of tuple

Discussion in 'Python' started by steph bagnis, Jun 9, 2004.

  1. steph bagnis

    steph bagnis Guest

    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
     
    steph bagnis, Jun 9, 2004
    #1
    1. Advertising

  2. steph bagnis

    Peter Hansen Guest

    steph bagnis wrote:

    > 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 ?


    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
     
    Peter Hansen, Jun 9, 2004
    #2
    1. Advertising

  3. steph bagnis

    Russell Blau Guest

    "Peter Hansen" <> wrote in message
    news:...
    > steph bagnis wrote:
    >
    > > 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 ?

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


    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.
     
    Russell Blau, Jun 9, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    962
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    616
    Bill Scherer
    Apr 20, 2004
  4. Davy
    Replies:
    3
    Views:
    1,893
    Wildemar Wildenburger
    Nov 7, 2007
  5. Jeff Nyman
    Replies:
    8
    Views:
    388
    Terry Reedy
    Jun 5, 2008
Loading...

Share This Page