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
alexvassel
10.8k3 gold badges33 silver badges32 bronze badges
3 Answers 3
new_lst = [x for t in lst for x in t[1:] if x != 0]
answered Jun 22, 2011 at 13:58
Sven Marnach
608k123 gold badges969 silver badges866 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
>>> [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
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Comments
>>> 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
inspectorG4dget
115k30 gold badges159 silver badges253 bronze badges
4 Comments
Sven Marnach
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 :)
inspectorG4dget
This is exactly what the interpreter gives me. How do you suggest that this is wrong?
Sven Marnach
For me, it gives
[u'1.15', u'1.15', 0, u'1.875', u'2.28', u'2.28'].inspectorG4dget
@Sven: you're right. I had to do an oldschool copy-paste (read and rewrite), and had missed that one
lang-py