Q. Enlist some advantages of list comprehensions.


Answer =

• Code reduction. A code of 3 or more lines (for loop with or without a condition) gets reduced to a single line of code.

• Faster code processing. List comprehensions are executed faster than their equivalent for loops for these two reasons:-
•  (a) Python will allocate the list's memory first, before adding the elements to it, instead of having to resize on runtime.
•  (b) Also, calls to append() function get avoided, reducing function overhead time (i.e., additional time taken to call and return from a function) which may be cheap but add up.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post