Example 1: list comprehension python
vec = [-4, -2, 0, 2, 4]
doubled = [x*2 for x in vec]
greater_thatn_0 = [x for x in vec if x >= 0]
positive = [abs(x) for x in vec]
freshfruit = [' banana', ' loganberry ', 'passion fruit ']
fruits_nospaces = [weapon.strip() for weapon in freshfruit]
squares = [(x, x**2) for x in range(6)]
^
vec = [[1,2,3], [4,5,6], [7,8,9]]
unpacking_tuple = [num for elem in vec for num in elem]
Example 2: python list comprehension
a = [1,2,3,4,5]
b = [5,6,7,8,9]
print([i for i in a if i not in b])
Example 3: list comprehension python 3
number_list = [x for x in range(10) if x % 2 == 0]
print(number_list)
Example 4: List Comprehensions
numbers = [1, 2, 3, 4, 5, 6]
squares = [i*i for i in numbers]
print(squares)
Example 5: list comprehension python
values = [2, 4, 6, 8, 10]
doubled_values = [x*2 for x in values]
print(doubled_values)
values = [2, 4, 6, 8, 10]
doubled_values = []
for x in values:
doubled_values.append(x*2)
print(doubled_values)
Example 6: Python List Comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)