-3

I am trying to extract data's from list of list of tuples but I am having an issue because they have different output from normal for loop and this list comprehension loop:

Here is my sample data and actually came from my database and return it as unicode and I just converted as a list:

[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]

And by using my simple for loop which is the correct output:

for i in datas:
    print i
[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]
[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]
[(2108, u'0930')]
[(3012, u'1')]

But if I will use the list comprehension style the out will be now different:

print [i for i in datas]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(1970, u'0238'), (1971, u'0243'), (2110, u'0929'), (2108, u'0930'), (1972, u'0932'), (1973, u'0934')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]
[[(2108, u'0930')], [(3012, u'1')]]

Any advice or suggestions thanks in advance!

Syntax Rommel
  • 932
  • 2
  • 16
  • 40
  • 3
    Your for-loop prints out individual lists. Your list comprehension *creates a big list*. Indeed, it is totally pointless, since `[i for i in datas] == list(datas) == datas` – juanpa.arrivillaga Feb 28 '18 at 00:49
  • Your plain `for` loop is correct, so I'm not sure why you want the listcomp at all. listcomps are a functional programming construct; using them for side-effects (e.g. `print`ing each element) is strongly discouraged. Just use the loop you've got. – ShadowRanger Feb 28 '18 at 02:52
  • Actually yes, my for loop is already working but what I am trying to do here is to improve it's execution time. I saw this article as my reference that using list comprehension might help to optimize the speed, so that's why I am trying with different solutions. https://stackoverflow.com/questions/16341775/what-is-the-advantage-of-a-list-comprehension-over-a-for-loop – Syntax Rommel Feb 28 '18 at 02:59

1 Answers1

0

by for loop, you are printing the items inside the list, but in list comprehension itself creating a outer list.

I think you need,

 print sum([i for i in datas],[])
Pyd
  • 6,017
  • 18
  • 52
  • 109
  • 1
    Do *not* use `sum([i for i in datas],[])`, it is an anti-pattern. The docs of `sum` specifically mention it is meant to be used with numeric types. Using it with a sequence type like a `list` will give you O(N^2) behavior, and you can do it with linear time complexity instead – juanpa.arrivillaga Feb 28 '18 at 01:04
  • I consider your suggestion but on this logic the expected result is still not the same. – Syntax Rommel Feb 28 '18 at 01:40
  • Adding to @juanpa.arrivillaga: If you need to do this, the correct approach is `list(itertools.chain.from_iterable(datas))`, which is `O(n)` (thanks to `list` appends being amortized `O(1)`). – ShadowRanger Feb 28 '18 at 02:51