# list Generation # list generation is list comprehensions, which is a very simple but powerful built-in import os# build that can be used to create a list of Python [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]l1 = list (range (1),  11)) print (' L1: ', l1) # generate [1*1, 2*2, 3*3, ..., 10*10]# 1. Using loops to generate L2 = []for x in range (1, 11): l2.append (x * x) Print (' L2: ', l2) # 2. List Generation L2 = [x * x for x in range (1, 11)]print (' L2: ', l2) # if condition filter only even squares l3 = [x * x for x in range (1, 11) if x % 2 == 0]print (' L3: ',  L3) # uses a two-layer loop to generate the full array L4 = [m + n for m in ' ABC ' for n in ' XYZ ']print (' L4: ',  L4) # lists all files and directory names under the current directory L5 = [d for d in os.listdir ('. ')] Print (' L5: ',  L5) # uses two variables to generate LISTD = {' x ': ' A ', ' y ': ' B ', ' z ': ' C '}l6 = [k + ' = ' + v for k, v in d.items ()]print (' L6: ', l6) # all the strings in a list into lowercase l = [' Hello ', ' World ', ' IBM ', ' Apple ']l7 = [s.lower () for s in L]print (' L7: ',  L7)
Python---List-generated