alexvassel
alexvassel

Reputation: 10740

Python, working with lists

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.

Upvotes: 1

Views: 1313

Answers (3)

inspectorG4dget
inspectorG4dget

Reputation: 114035

>>> 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

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

>>> [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']

Upvotes: 2

Sven Marnach
Sven Marnach

Reputation: 602725

new_lst = [x for t in lst for x in t[1:] if x != 0]

Upvotes: 4

Related Questions