list of tuple

Russell Blau russblau at hotmail.com
Wed Jun 9 08:59:07 EDT 2004


"Peter Hansen" <peter at engcorp.com> wrote in message
news:TJKdnUm2dcExb1vdRVn-gQ at powergate.ca...
> 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.





More information about the Python-list mailing list