Why can't I append to list inside of a list comprehension?
I'm new to programming a bit, and am learning python so I can learn flask, using the python crash course book. I was learning about list comprehension but it briefly talks about it. If I do
list[list.append(value) for value in range(1, 20)]
it doesn't work. Would this be some sort of recursive expression that is not possible?
List comprehensions return a new list. For the sake of code clarity, you probably shouldn't change a second list from within a list comprehension. If you're trying to concatenate two lists, you can do so in a second line:
a = list(range(10))
b = [ value for value in range(5) ]
a.extend(b)
# a has 15 elements
print(a)
list.append returns None so what you've actually got is a list comprehension that generates a list containing the value None 19 times. (using functions with side effects, such as list.append, in list comprehensions are generally bad style so you should avoid this)
The list[...] syntax retrieves elements from the list, which is not what you're trying to do here. (and it is actually invalid syntax in this case)
You should generally avoid calling lists list, because list is already a builtin.
If you want to append the numbers 1 to 19 to a list as you're trying to do you can call the list.extend function with the list comprehension [value for value in range(1, 20)] as the argument. (Although in this case you can also just use the range directly.) To do it without list comprehensions you can simply loop over the range and repeatedly call the append function.
A list comprehension is used to convert and/or filter elements of another iterable, in your case a range but this could also be another list. So you can think of it as taking one list, filtering/converting each element and producing a new list as a result.
So there's no need to append to any list as that's implicit in the comprehension.
For example, to produce a list of all squares in a range you could do:
[x*x for x in range(10)]
This would automatically "append" each square to the resulting list, there's no need to do that yourself.
List comprehension is not whatever you're doing there. An example of list comprehension:
list = [value*2 for value in range(1, 20)]
See, list comprehension is used to make a list from an existing list. The value of the new list is defined by a function. In this case, the value of a will be 2,4,6, etc.
Your current syntax list[...], is trying to access an element of a list.
You can. Whatever the method returns will be the element of that list. So if for example I do this:
def mul(x):
return x*2
list = [mul(value) for value in range(1,20)]
It will have the same effect. But this:
def mul(x):
return
list = [mul(value) for value in range(1,20)]
Will just makes the list element all None
Edit to add more:
List comprehension works not from the range function. Rather, the range function is returning a list. Hence the name, "list comprehension". You can use any old list for it.
What it did under the hood is that it iterates each element on the list that you specify (the in ...), and applies those to the function that you specify in the very first place. If you are familiar with the concept of Array.map in other languages, this is that. There is also a technical explanation for it if it helps, but it requires more time to explain. Just let me know if you would like to know it.
This says I want to make mylist be a list where each element of the list (called value here) comes from doing a for loop on range, given the parameters 1, and 20.
If you want to change how each element of this list is, you do it in the first bit on “value”
So you could do
mylist = [value*5 for value in range(1,20)] //5,10,15,…,95 (not 100, because ranges go up to
the last item, not including it (non-inclusive))