What is the difference between normal loops and list comprehension?

x = 'aaaabbbccd'
new = list(itertools.groupby(x))
[print(i) for i in new]
for i in new:
    print(i)

The result for line 2 is is something like:

('a', <itertools._grouper object at 0x0000014163062EB0>)
('b', <itertools._grouper object at 0x0000014163062FD0>)
('c', <itertools._grouper object at 0x0000014163062F70>)
('d', <itertools._grouper object at 0x0000014162991BB0>)
[None, None, None, None]

Where as the result for the normal for loop is:

('a', <itertools._grouper object at 0x0000014163062EB0>)
('b', <itertools._grouper object at 0x0000014163062FD0>)
('c', <itertools._grouper object at 0x0000014163062F70>)
('d', <itertools._grouper object at 0x0000014162991BB0>)

Why do I get the extra [None, None, None, None] in case of list comprehension?

Upvotes: 0

Views: 707

Answers (4)

aakriti
aakriti

Reputation: 158

Generally speaking, the list comprehensions are more efficient both computationally and in terms of coding space and time than a for-loop. Typically, they are written in a single line of code. read this article

as demoed in the article and I'm quoting here!:

import timeit

def squares(size):
    result = []
    for number in range(size):
        result.append(number*number)
    return result

def squares_comprehension(size):
    return [number*number for number in range(size)]

print(f""" Timed using for loop: {timeit.timeit("squares(50)", "from __main__ import squares", number = 1_000_000)}""")
print(f""" Timed using for List Comprehension: {timeit.timeit("squares_comprehension(50)", "from __main__ import squares_comprehension", number = 1_000_000)}""")

output:

Timed using for loop: 6.206269975002215
Timed using for List Comprehension: 4.1636438860005
  • List comprehensions are often not only more readable but also faster than using “for loops.” They can simplify your code, but if you put too much logic inside, they will instead become harder to read and understand.

  • Even though list comprehensions are popular in Python, they have a specific use case: when you want to perform some operations on a list and return another list. And they have limitations - you can’t break out of a list comprehension or put comments inside. In many cases, “for loops” will be your only choice. read this article

also, faster is not always the case! If iterations are performed over computationally expensive functions, list and for-loop runtime may be almost the same. an awesome read

Upvotes: 0

user16377193
user16377193

Reputation:

its reason is on list comprehension code creates a list but you dont put value into list so code puts None but on normal loop code doesn't create a list

Upvotes: 0

PCM
PCM

Reputation: 3011

A list comprehension is used to comprehend (Make) a list. It is useful only when making lists. However, here you are not making a list, so it is not recommended to use list comprehension. You only print the value and not store it as a list. Here, use a for a loop.

The reason you get None is - the list comprehension basically becomes a list of print() functions like [print(...),print(...)....]

So when you call them it becomes like - print(print(...)), which, if you try this code, will return a None along with the output.

So, do not use list comprehension unless you are using it to build a list.

References - This and That

Upvotes: 1

PJA
PJA

Reputation: 26

When you write a calculation in the interpreter, the result is printed back. Your list comprehension result was '[None, None, None, None]', because print result value is None, and therefor was printed.

>>> 1+1
2
>>> 2
2
>>> [1,2,3]
[1, 2, 3]
>>> [None for i in new]
[None, None, None, None]
>>>

Upvotes: 0

Related Questions