List comprehension

​ Instead of for loops, we should use list comprehension, equivalently,

[expression for item in iterable if condition == True]

​ Why? so simple. Let’s just assume that we want to print out all the letters in ‘google’ as a list.

​ If we use for loop,

a = []

for letter in "google":
  a.append(letter)

​ On the other hand, with the list comprehension,

a = [letter for letter in "google"]

​ As we can see clearly, the latter is simpler and it is definitely powerful when the function gets more complex. Thus, we should be accustomed to the list comprehension.

​ Let’s practice with some examples.

l = [22, 13, 45, 50, 98, 69, 43, 44, 1]
j = [12, 42, 52, 13, 24, 53, 94, 24, 25]
k = [['A', 'B', 'C'] ,['D', 'E', 'F'], ['G', 'H', 'I']] 1. Subtract elements smaller than 30 in l  
   [x for x in l if x < 30]

the output is,

    [22, 13, 1]
  1. Tell if an element is smaller than 30 in l l

    [x < 30 for x in l]
    

    the output is,

     [True, True, False, False, False, False, False, False, True]
    
  2. Extract elements in l which are bigger than those in j element-wisely

    [x for i, x in enumerate(l) if x > j[i]]
    

    the output is,

      [22, 50, 98, 69, 44]
    
  3. Return element+10 if it is bigger than j element-wisely, otherwise return element-10

    [x+10 if x > j[i] else j[i]-10 for i, x in enumerate(l)]
    

    the output is,

    [32, 32, 42, 60, 108, 79, 84, 54, 15]
    
  4. Change the middle value of each nested list in k to lower case

    [[val.lower() if i ==1 else val for i, val in enumerate(x)] for x in k]
    

    the output is,

      [['A', 'b', 'C'] ['D', 'e', 'F'], ['G', 'h', 'I']]