+ 1
List comprehensions always better than functionnal programming (filter, map, ...) ?
I am learning Python and I asked this question to myself. I have got a starting of answer : List comprehensions is more readable, and faster (see: https://youtu.be/YjHsOrOOSuI?t=695). Moreover with the topic : "Why use lambda when you can use list comprehension?" and this code : https://code.sololearn.com/caVGRw4he3EQ/?ref=app#py That's why I think that list comprehension should be used at any times. Am I wrong ? Why should we keep using functionnal programming (just for multiple paradigms ?) ?
5 Respostas
+ 10
just to share something interesting that I came across recently. Even if list comprehension is the most famous there are also:
{key: value for ....} - dictionary comprehension and more interesting
(x for x ....) - not tuple comprehension as one might think, actually that creates a generator 😀😀😀
+ 9
All are different things, actually:
List comprehensions are used to construct lists. Of any type, of any dimension, convertible to other data types, but.. essentially lists, exclusively.
Functional programming paradigm is a whole another story. It is a way of programming and code flow that resembles a bit mathematical thinking, converting one value to another. Data type is not so important here, but the logic is to get an output which will carry some value (often being an argument to another function, when chaining).
Lambdas are just anonymous functions, that is used when there is an ad hoc necessity, with no future re-use planned.
+ 2
To complete :
{value for ....} - set comprehension 😀
+ 1
Yes, you are right in some cases but if u want to create a function that you want to use many times in a program then you have to use lambda function for creating the function. Also it is only efficient if you are using the same function for a number of inputs.
0
Ok thanks. Both are useful then.