list of tuple

Peter Hansen peter at engcorp.com
Wed Jun 9 07:21:15 EDT 2004


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



More information about the Python-list mailing list