Take the 2-minute tour ×

This question already has an answer here:

How can I extract the specific tuples from the lists given below:

[[[(1, 4)]], [[(1, 3)], [(5, 4)]], [[(1, 2)]]]

I want to extract the tuples with SAME x-axis, like:

[(1,4), (1,3), (1,2)]

whereas (5,4) should be discarded. Please help me.

share|improve this question

marked as duplicate by root, DocMax, Joel Cornett, Martijn Pieters, dawg Feb 23 '13 at 16:48


What have you tried? You'll be more likely to get helpful answers if you show that you've already done some of the work yourself (both because other people are more likely to try to help you out, and because it can show more details of your situation. –  Blckknght Feb 23 '13 at 6:39
How is this different to your previous question? –  Johnsyweb Feb 23 '13 at 6:41
@ Johnsyweb: I am new to python! trying to make a project of mine! I need to extract these values for my further calculations! list in list scares me alot. –  Huzaifa Shaikh Feb 23 '13 at 6:46
@ Blckknght: I have used nested loops to extract them and then compare values, but this makes execution very slow! I want a fast extraction method so that I may get and compare values from a sheet of 16K rows. –  Huzaifa Shaikh Feb 23 '13 at 6:49
what do you mean with "sheet with 16K rows"? you should tell us whats the initial input (excel sheet?) and what are you trying to achieve...because there is probably a better/faster way to do it all together... –  root Feb 23 '13 at 6:56

2 Answers 2

up vote 1 down vote accepted

To flatten a list, you should always use itertools.chain. In this case, you have to apply nested chain to create a flat list of tuples

>>> [e for e in chain(*chain(*l)) if e[0] == 1]
[(1, 4), (1, 3), (1, 2)]
share|improve this answer
You should actually use chain.from_iterable(l) vs chain(*l). Faster. A list comprehension is also faster vs chain(*l) but chain.from_iterable(l) is faster than a LC. See the timing. –  dawg Feb 23 '13 at 18:10

Well you added an additional depth of list compared to your previous question

So, this list comprehension works:

>>> def flat1(l): return [i for sub in l for i in sub]
>>> [t for t in flat1(flat1(l)) if t[0]==1]

The list comprehension method of flattening a list of lists if about the same speed (slightly faster) as itertools.chain(*l).

If you want to use itertools for speed, you should use chain.from_iterable(l) instead.

Here is the timing:

import timeit

def f1(l):
    return [e for e in itertools.chain(*itertools.chain(*l)) if e[0] == 1]


def f2(l):


def f1(l):
    return [e for e in flat1(flat1(l)) if e[0] == 1]


t1=timeit.timeit(stmt=c1,setup='import itertools',number=n)
t3=timeit.timeit(stmt=c3,setup='import itertools',number=n)

print '          chain(*l):',t1,'seconds'
print ' list comprehension:',t2,'seconds'
print 'chain.from_iterable:',t3,'seconds'


          chain(*l): 4.32919406891 seconds
 list comprehension: 4.32601380348 seconds
chain.from_iterable: 3.14966917038 seconds
share|improve this answer
