+ 4
What is the use of comprehension in python
plz, let me know about comprehension,what it is actually???
3 Réponses
+ 8
Comprehensions are the 'pythonic' way to create lists, sets or dictionaries. For example, to create a list containing the squares of 0-9, you can do the same thing as in every other programming language; create an empty list, iterate over the numbers 0-9 and append their squares to the list:
squares = []
for i in range(10):
squares.append(i**2)
Or you can use a list comprehension and do everything in one step:
squares = [i**2 for i in range(10)]
+ 4
thank you so much