Professional Documents
Culture Documents
Python Comrehensions
Python Comrehensions
Python Comrehensions
values = []
# for x in range(10):
# values.append(x)
print(values)
#Condition comrehension
evens = []
# for number in range(50):
# is_even = number % 2 ==0
# if is_even:
# evens.append(number)
print(evens)
options = ["any","albany","apple","world","hello",""]
valid_strings = []
valid_strings = [
string
for string in options
if len(string) >= 2
if string[0] == "a"
if string[-1] == "y"
]
print(valid_strings)
print(flattened)
#if else comprehension
categories=[]
# for number in range(10):
# if number % 2 == 0:
# categories.append("Even")
# else:
# categories.append("Odd")
print(categories)
printer = pprint.PrettyPrinter()
lst = []
# for a in range(5):
# l1 = []
# for b in range(5):
# l2 = []
# for num in range(5):
# l2.append(num)
# l1.append(l2)
#
# lst.append(l1)
printer.pprint(lst)
#transformation in comprehension
def square(x):
return x**2
print(squared_number)
#Dictionary comprehension