Ordered Dictionary and Sorting
<p>I'm trying to solve a simple practice test question:</p>

<blockquote>
  <p>Parse the CSV file to:</p>
  
  <ul>
  <li>Find only the rows where the user started before September 6th, 2010. </li>
  <li>Next, order the values from the "words" column in ascending order (by start date) </li>
  <li>Return the compiled "hidden" phrase</li>
  </ul>
</blockquote>

<p>The csv file has 19 columns and 1000 rows of data. Most of which are irrelevant. As the problem states, we're only concerned with sorting the the start_date column in ascending order to get the associated word from the 'words' column. Together, the words will give the "hidden" phrase.</p>

<p>The dates in the source file are in UTC time format so I had to convert them. I'm at the point now where I think I've got the right rows selected, but I'm having issues sorting the dates. </p>

<p>Here's my code:</p>

<pre><code>import csv
from collections import OrderedDict
from datetime import datetime


with open('TSE_sample_data.csv', 'rb') as csvIn:
    reader = csv.DictReader(csvIn)
    for row in reader:

        #convert from UTC to more standard date format
        startdt = datetime.fromtimestamp(int(row['start_date']))
        new_startdt = datetime.strftime(startdt, '%Y%m%d')        

        # find dates before Sep 6th, 2010
        if new_startdt &lt; '20100906':

            # add the values from the 'words' column to a list 
            words = []
            words.append(row['words'])

            # add the dates to a list
            dates = []
            dates.append(new_startdt)

            # create an ordered dictionary to sort the dates... this is where I'm having issues 
            dict1 = OrderedDict(zip(words, dates))
            print dict1
            #print list(dict1.items())[0][1]
            #dict2 = sorted([(y,x) for x,y in dict1.items()])
            #print dict2
</code></pre>

<p>When I <code>print dict1</code> I'm expecting to have one ordered dictionary with the words and the dates included as items. Instead, what I'm getting is multiple ordered dictionaries for each key-value pair created.</p>

<p>Here's the corrected version:</p>

<pre><code>import csv
from collections import OrderedDict
from datetime import datetime


with open('TSE_sample_data.csv', 'rb') as csvIn:
    reader = csv.DictReader(csvIn)
    words = []
    dates = []
    for row in reader:

        #convert from UTC to more standard date format
        startdt = datetime.fromtimestamp(int(row['start_date']))
        new_startdt = datetime.strftime(startdt, '%Y%m%d')        

        # find dates before Sep 6th, 2010
        if new_startdt &lt; '20100906':

            # add the values from the 'words' column to a list 
            words.append(row['words'])
            # add the dates to a list
            dates.append(new_startdt)

    # This is where I was going wrong! Had to move the lines below outside of the for loop
    # Originally, because I was still inside the for loop, I was creating a new Ordered Dict for each "row in reader" that met my if condition
    # By doing this outside of the for loop, I'm able to create the ordered dict storing all of the values that have been found in tuples inside the ordered dict
    # create an ordered dictionary to sort by the dates
    dict1 = OrderedDict(zip(words, dates))
    dict2 = sorted([(y,x) for x,y in dict1.items()])

    # print the hidden message
    for i in dict2: 
        print i[1]
</code></pre>

python sorting for-loop dictionary ordereddictionary