Interesting Exercise for List Comprehension

DataCamp’s tutorial gave me a challenging exercise which I felt it should be share here. Using the data structure, the Pandas Series, the exercise pre-loaded a .csv file as a DataFrame for analyzing data from the Pandas DataFrame.

You can think of DataFrame columns as single-dimension arrays called Series.

The below example uses the DataFrame, df and list comprehension to extract the 12th to 19th characters in the string of column “created_at” to extract the time. The second print() statement uses conditional list comprehension to check,

entry[17:19] is equal to ’19’.

The code is as below:

# Extract the created_at column from df: tweet_time
tweet_time = df['created_at']

# Extract the clock time: tweet_clock_time
tweet_clock_time = [entry[11:19] for entry in tweet_time]

# Print the extracted times

# Access the 12th to 19th characters in the string to extract the time. Add a conditional expression that checks whether entry[17:19] is equal to '19'.
tweet_clock_time = [entry[11:19] for entry in tweet_time if entry[17:19] == '19']

# Print the extracted times

Both print() statements print out the output of variable, “tweet_clock_time”. It contains the timing of each records found in the .csv file with different conditions. The second print() statement print out records that ended with 19 seconds only.


Revisit List Comprehensions in Python

I found myself caught in the maze of list comprehensions and I took about 30 minutes to read and fully understand the instructions given by the instructor and test the result before submitting my answer in the DataCamp’s exercise which requires me to create a matrix as below using the list comprehension method I learned.

The matrix,

matrix = [[0, 1, 2, 3, 4],
          [0, 1, 2, 3, 4],
          [0, 1, 2, 3, 4],
          [0, 1, 2, 3, 4],
          [0, 1, 2, 3, 4]]

Here is the instructions:

1. In the inner list comprehension – that is, the output expression of the nested list comprehension – create a list of values from 0 to 4 using range(). Use col as the iterator variable.
2. In the iterable part of your nested list comprehension, use range() to count 5 rows – that is, create a list of values from 0 to 4. Use row as the iterator variable; note that you won’t be needing this to create values in the list of lists.

[[output expression] for iterator variable in iterable]

The code uses the above syntax to construct.

Before this, I skipped the instructions and tried on my hand and it failed few times. Then, I read it once again but I did not get it right as I misunderstood the meaning of “inner list comprehension”, that is the first list comprehension that I need to put inside the “output expression” for col.

Then, followed with the another list comprehension for row.

The matrix’s code looks as below:

matrix = [[col for col in range(0,5)] for row in range(0,5)]

Finally, I am able to create the above matrix.

Summary of the day:

  • List comprehensions
  • Iterable, Iterator
  • Matrix