List comprehension in Python
Code
Example 1:
l = [i for i in range(1, 11, 1)]
print(l)
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 2:
l = [[i, i+1] for i in range(1,6)]
print(l)
Output: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
Example 3:
l = [i for i in range(1, 11, 1)]
print(l)
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 4:
l = [i for i in range(1,6) if i%2 == 0]
print(l)
Output: [2, 4]
Comments
Post a Comment