
# Frequently, you’ll want to transform a list into another list, 
# by:
#
#   (1)  choosing only certain elements, or 
#   (2)  by transforming elements, or 
#   (3) both. 
#
#  The Pythonic way of doing this is list comprehensions:
#
#    [ expr  for item in LIST  if boolean ]
#
#    Construct a NEW list with:
#
#          (1) select: item  in LIST that satisfy "boolean"
#          (2) compute "expr" with these items
#          (3) return a LIST with these expressions

even_numbers = [x for x in range(5) if x % 2 == 0] 	# [0, 2, 4]

squares = [x * x for x in range(5)] 			# [0, 1, 4, 9, 16]

even_squares = [x * x for x in even_numbers] 		# [0, 4, 16]


# You can transform a list into a dictionary or a set too:

square_dict = { x : x * x for x in range(5) } 	# { 0:0, 1:1, 2:4, 3:9, 4:16 }
square_set = { x * x for x in [1, -1] } 	# { 1 }


# ######################################################

# Example:

zeroes = [ 0  for  x in range(5) ]		# [0,0,0,0,0]
print(zeroes)





# ##########################################################
# Nested for loops

pairs = [(x, y)
    for x in range(3)
    for y in range(2)] 

print(pairs)

# ##############################################################
# Later fors can use the result of EARLIER ones !!!

increasing_pairs = [(x, y) 	# only pairs with x < y,
    for x in range(5) 		# x = 0, 1, 2, 3, 4
    for y in range(x + 1, 5)]	# y = x+1, x+2, ..., 4

print(increasing_pairs)


