list comprehensions

0

I am learning python and I have discovered the concept of "list comprehensions" (I do not know if it has a Spanish translation).

I was doing tests to understand it and I did not finish clarifying myself.

For example, if I want to show the even values between a range of values (0 to 25 for example), I would know how to do it in the "normal" way, but with this method I would not: (

pares =[]
for i in range (0,26):
    if i%2 ==0:
        pares.append(i)
print(pares)

But with "list comprehensions" I do not get it, this is my last test,

pares = [x for x range(0,26) if x%2 ==0]
print (pares)

Health and thanks!

    
asked by NEA 07.10.2018 в 13:08
source

1 answer

2

Your approach was correct, you have only made a silly mistake of syntax and that you missed the in that the iteration allows:

pares = [x for x in range(0,26) if x%2 ==0]
print (pares)

I take the answer to add that " list comprehension " is officially translated by understanding of lists . At least that is how it appears translated in the official python tutorial . Personally I never use that translation because it seems horrible to me and I have the feeling that nobody will understand me, but they are my things anyway.

    
answered by 07.10.2018 / 13:22
source