+ 9

Can anyone explain me what is list comprehension in python with easy example?

9th Nov 2020, 11:48 AM
#It's Tony Stark
#It's Tony Stark - avatar
2 Respuestas
+ 6
its making a list (or other containers) without several lines of code. nums = [1,2,3,4,5] # ====== getting squared values ===== # regular syntax squ = [] for n in nums: squ.append(n*n) OR # list comp squ1 = [n*n for n in nums] BOTH squ and squ1 will contain the squared values of 1-5: [1,4,9,16,25]
9th Nov 2020, 12:21 PM
Slick
Slick - avatar
+ 10
#It's Tony Stark , a while ago, i did a small tutorial how to turn a regular for loop to a comprehension: https://code.sololearn.com/cqnGujvL7CXe/?ref=app
9th Nov 2020, 3:43 PM
Lothar
Lothar - avatar