{ List Exercises. }

Part I

Write the following Python code to do the following (complete ALL of these using list comprehension).

  1. Given a list [1,2,3,4], print out all the values in the list.
  2. Given a list [1,2,3,4], print out all the values in the list multiplied by 20.
  3. Given a list ["Elie", "Tim", "Matt"], return a new list with only the first letter (["E", "T", "M"]).
  4. Given a list [1,2,3,4,5,6] return a new list of all the even values ([2,4,6]).
  5. Given two lists [1,2,3,4] and [3,4,5,6], return a new list that is the intersection of the two ([3,4]).
  6. Given a list of words ["Elie", "Tim", "Matt"] return a new list with each word reversed and in lower case (['eile', 'mit', 'ttam']).
  7. Given two strings "first" and "third", return a new string with all the letters present in both words (["i", "r", "t"]).
  8. For all the numbers between 1 and 100, return a list with all the numbers that are divisible by 12 ([12, 24, 36, 48, 60, 72, 84, 96]).
  9. Given the string "amazing", return a list with all the vowels removed (['m', 'z', 'n', 'g']).
  10. Generate a list with the value [[0, 1, 2], [0, 1, 2], [0, 1, 2]].
  11. Generate a list with the value:

    [
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    ]
    

For solutions to these exercises, click here.

When you're ready, move on to Dictionary Basics

Continue

Creative Commons License