+ 5

Dynamic list creation

I want to create a list, in which each element depends on the preceding one. https://code.sololearn.com/c3oyUCYdB7W0/?ref=app I tried with a regular loop and with a list comprehension. The results are very different! (1) how can we explain the comprehension behaviour? (2) is possible to obtain a result like the one with a regular loop, but using a comprehension?

17th Nov 2019, 7:08 AM
Bilbo Baggins
Bilbo Baggins - avatar
10 Answers
+ 3
This is not really what you asked for but some sort of answer nonetheless. You can create a sort of dummy list comprehension which modifies an original list. See code. https://code.sololearn.com/c3ZcdfI0WNa5/?ref=app
17th Nov 2019, 10:01 AM
Russ
Russ - avatar
+ 5
I see how a technique like that may help you to write oneliners - I may resort to it later myself. 😉 But in my opinion, if brevity, terse, concise become the only goal, even in cases where you're performing operations that are not needed and the code actually becomes harder to understand, you're crossing the borderline to abusing a language. If Python has one thing really going for it, it's its potential to be *readable*, and foremost, I think, we should concentrate on accomplishing that.
17th Nov 2019, 1:15 PM
HonFu
HonFu - avatar
+ 4
You can't use append method in comprehension. Here is how to do it: https://code.sololearn.com/ceYms0FKs8xk/?ref=app
17th Nov 2019, 7:15 AM
Asman-H
Asman-H - avatar
+ 3
I think the original code example has changed ro when you wrote the question? In a regular loop you can append to the list from the loop, because the list and its name already exists. A comprehension is more like 'make me a list that...' What you want to do is like: 'Make me a list that's like the list that doesn't exist.' Do you maybe have a more realistic example, like a real life use case for what you want to do? Maybe it will become easier to look for a trick...
17th Nov 2019, 9:53 AM
HonFu
HonFu - avatar
+ 3
The code inside the comprehension expression, is actually an iterable, not a list, so you cannot slice it and cannot refer back to its last element. It becomes a list on effect of the [ ] brackets after fully evaluated. You can also create sets or dictionaries by comprehension, or pass the iterable expression directly to a function like sum(). One way around this is using generators to calculate the next value dynamically. An example: https://code.sololearn.com/c3DpX5B6JhXZ/?ref=app https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2462/
17th Nov 2019, 11:08 AM
Tibor Santa
Tibor Santa - avatar
+ 3
But why do you need it? Why not a normal loop? I mean, you're creating a list full of None. What's the profit of doing that?
17th Nov 2019, 11:47 AM
HonFu
HonFu - avatar
+ 2
Russ that was exactly what I tried to obtain. Here is an application: https://code.sololearn.com/cNx9WhhrCsgG/?ref=app
17th Nov 2019, 10:52 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
Thanks, Asman-H , you are right, I am wrong regarding "append", it returns None. But you are somehow cheating in your solution. I would like to refer, in comprehension, not to the first static element, but to the previously appended one. Here is a more complex example: https://code.sololearn.com/cW5KtZ3aJ0dM/?ref=app
17th Nov 2019, 7:51 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
I think the original code example has changed in comparison with when you wrote the question? In a regular loop you can append to the list from the loop, because the list and its name already exists. A comprehension is more like 'make me a list that...' What you want to do is like: 'Make me a list that's like the list that doesn't exist.' Do you maybe have a more realistic example, like a real life use case for what you want to do? Maybe it will become easier to look for a trick...
17th Nov 2019, 9:54 AM
HonFu
HonFu - avatar
+ 1
HonFu maybe love for conciseness (generators require a function definition, loops... yes but in that case I prefer writing in C)? Maybe love for pseudo oneliners or different solutions? Maybe masochism?
17th Nov 2019, 12:52 PM
Bilbo Baggins
Bilbo Baggins - avatar