list of tuple

S

steph bagnis

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
 
P

Peter Hansen

steph said:
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
 
R

Russell Blau

Peter Hansen said:
steph said:
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] == '+']
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top