I come from a C/C++ background and I can't find out how this works in python.
In C++, return values are stored in a temporary variable and can be used/reassigned. So I can do something like this:
int foo() {
return 1;
}
int main() {
int x = foo() + 1;
return 0;
}
I thought this works the same in Python and until now, it did. But how come this won't work when using list comprehension:
print([i for i in range(5)].append(5))
I expected a list from 0-5, but I get None.
To work around this problem, I need to first assign my list to a variable:
my_list = [i for i in range(5)]
my_list.append(1)
print(my_list)
So list comprehensions creates new list from some iterator but doesn't return the new list? But somehow if I assign it to a variable it does return the new list? Confused
Thanks for reading.