List Comprehensions
List making style..
List comprehension is a powerful way to create lists.
syntax::
L=[i for i in range(n)]
Here is a simple example given..
string="hello"
L=[1,4,5,9,12]
M=["one", "two", "three", "four", "five"]
Lists-------
[o for i in range(100)]
output=[0,0,0,0,0,0,0,0,0,0]
L=[i for i in range(n)]
Here is a simple example given..
string="hello"
L=[1,4,5,9,12]
M=["one", "two", "three", "four", "five"]
Lists-------
[o for i in range(100)]
output=[0,0,0,0,0,0,0,0,0,0]
[i**2 for i in range(1,8)]
output=[1,4,9,16,25,36,49]
[i*10 for i in L]
output=[10,40,90,120]
[c*2 for c in string]
output["hh", "ee", "ll", "ll", "oo"]
[M[0] for m in M]
output=["o", "t", "t", "f", "f"]
[i for i in L if i<10]
output=[1,4,5,9]
😎😎😎😎😎😎
Comments
Post a Comment