Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (40 sloc) 1.36 KB
# list comprehension
# we can populate a list using append as follows:
NEWLIST_LOOP = []
for counter in range(10):
NEWLIST_LOOP.append(counter)
print(NEWLIST_LOOP)
# these three lines fills NEWLIST with these values:
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# The above code has a side effects, it creates or overwrites
# the variable named counter
# instead we can use list comprehension, the same list will be generated by
NEWLIST_COMP = [x for x in range(10)]
# one line vs 3, and without side effects. This is the pythonic
# way of list generation
print(NEWLIST_COMP)
# naturally you can do more complex lists:
LIST_COMP_2 = [x**2 for x in range(10)]
print(LIST_COMP_2)
#imagine you want to create a new list containing all elements of list_comp_2 that are even
# with normal code:
LIST_COMP_3 = []
for elem in LIST_COMP_2:
if elem % 2 == 0:
LIST_COMP_3.append(elem)
print(LIST_COMP_3)
#the same list using list comprehension
LIST_COMP_4 = [x for x in LIST_COMP_2 if x%2 == 0]
print(LIST_COMP_4)
print('qui')
# last example all elements contained in both LIST_COMP_2 and NEWLIST_COMP
# that are even
LIST_COMP_5 = [x for x in LIST_COMP_2 if x in NEWLIST_COMP and x%2 == 0]
print(LIST_COMP_5)
# the same list without using list comprehension
LIST_6 = []
for elem in LIST_COMP_2:
if elem in NEWLIST_COMP:
if elem%2 == 0:
LIST_6.append(elem)
print(LIST_6)