python comprehension examples
Example 1: python list comprehension
nums = [4, -7, 9, 1, -1, 8, -6]
half_of_nums = [x/2 for x in nums]
half_of_positive_nums = [x/2 for x in nums if x>=0]
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
list_comp = [i+3 for i in range(20)]
for i in range(20):
print(i + 3)
Example 4: Python List Comprehension
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)