CBSE Class 12 Computer Science Question 37 of 68

Data Structures - I : Linear Lists — Question 9

Back to all questions
9
Question

Question 9

Enlist some advantages of list comprehensions.

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).