Computer Science

Enlist some advantages of list comprehensions.

Linear Lists

3 Likes

Answer

Advantages of list comprehensions are as follows:

  1. Code reduction — A code of 3 or more lines (for loop with or without a condition) gets reduced to a single line of code.
  2. Faster code processing — List comprehensions are executed faster than their equivalent for loops for these two reasons:
    1. Python will allocate the list's memory first, before adding the elements to it, instead of having to resize on runtime.
    2. Also, calls to append() function get avoided, reducing function overhead time (i.e., additional time taken to call and return from a function).

Answered By

2 Likes


Related Questions