+ 1
Hello. Can someone explain it to me?
I see a python-challenge: List comprehension can be used to ... ? Tge right answer is ' construct a list'.
4 Answers
+ 5
You can fill a list with items, for example:
list_ = []
for i in range(10):
list_.append(int(input())
This creates a list and after that fills it with ten numbers the user inputs.
Now a list comprehension is a shortcut:
list_ = [int(input()) for i in range(10)]
+ 2
I don't know how the term was intended originally. I understand it like:
'A comprehensive way to build a list'
(like an all-in-one package, you don't have to do each step separately).
You can use it for other container types as well:
# assuming you have two containers keys and values
{key: value for key in keys for value in values}
tuple(n * 2 for n in range(100))
etc.
+ 1
What does it mean this term?: List comprehension?
+ 1
I have understood . It is a generator. Thank you.