0

My professor used the following code:

    def eq(x):
     return x+1

    test = [1,2,3,4,5,6]
    result = [eq(each_solution) for each_solution in test]

It works as we would think, result is:

    [2, 3, 4, 5, 6, 7]

I expected this output but I don't know how or why the line

    result = [eq(each_solution) for each_solution in test]

work and should work, as usual we would put the eq(each_solution) in the for (although not like that inside a list), not behind. if we just put the line eq(each_solution) for each_solution in teste as expected, it won't work. But in a list [] it functions properly, why? I've never seen a For like that before.

Also what would you recommend to do if not that? There are other options?

  • 1
    This is a [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) and if you haven't seen it before, you probably haven't been coding Python for too long. It is one of the most common and useful features. – user2390182 Nov 19 '21 at 14:08

0 Answers0