1

I have such list of tuples:

lst = [(10, u'1.15', u'1.15'), (5, 0, u'1.875'), (3, u'2.28', u'2.28')]

and I want to get the new one with just second and third element of each tuple, which not equal to the 0, in other words, I need something like:

new_lst = [u'1.15', u'1.15',u'1.875', u'2.28', u'2.28']

Thanks for your answers.

asked Jun 22, 2011 at 13:56

3 Answers 3

4
new_lst = [x for t in lst for x in t[1:] if x != 0]
answered Jun 22, 2011 at 13:58
Sign up to request clarification or add additional context in comments.

Comments

2
>>> [x for y in lst for x in y[1:3] if x]
[u'1.15', u'1.15', u'1.875', u'2.28', u'2.28']
answered Jun 22, 2011 at 13:58

Comments

0
>>> L = [(10, u'1.15', u'1.15'), (5, 0, u'1.875'), (3, u'2.28', u'2.28')]
>>> answer = []
>>> for tup in L:
... answer.extend([i for i in tup[1:] if i])
... 
>>> answer
[u'1.15', u'1.15', u'1.875', u'2.28', u'2.28']

Hope this helps

answered Jun 22, 2011 at 13:59

4 Comments

Strange -- this looks like you typed it in the interactive interpreter, but you obviously didn't, since the result is incorrect. I'm really curious how this happened :)
This is exactly what the interpreter gives me. How do you suggest that this is wrong?
For me, it gives [u'1.15', u'1.15', 0, u'1.875', u'2.28', u'2.28'].
@Sven: you're right. I had to do an oldschool copy-paste (read and rewrite), and had missed that one

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.