Computer Science
Enlist some advantages of list comprehensions.
Linear Lists
2 Likes
Answer
Advantages of list comprehensions are as follows:
- 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:
- Python will allocate the list's memory first, before adding the elements to it, instead of having to resize on runtime.
- 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
Suggested situations where you can use these data structures:
(i) linear lists
(ii) stacks
(iii) queues
What is a list comprehension ? How is it useful ?
In which situations should you use list comprehensions and in which situations you should not use list comprehensions ?
What is a nested list ? Give some examples.