python list - method instead of string
Question: I am playing a bit with AWS via python and boto. I am trying to get modified
date for keys from AWS bucket. After that I am parsing date to 'regular' date
format and try to add every value to list.

Unfortunately, when I append values to list and try to print it's results I am
getting `datetime` function instead of string with date. Code looks like this:

    
    
    import boto
    s3 = boto.connect_s3()
    bucket = s3.get_bucket('mybucket')
    keys = bucket.list
    keys_latest_modified_date_list = []
    
    for key in keys:
        key_latest_modified_date = boto.utils.parse_ts(key.last_modified)
        keys_latest_modified_date_list.append(key_latest_modified_date)
        print key_latest_modified_date
    print keys_latest_modified_date_list
    

First `print` returns correct date:

    
    
    2015-02-18 10:11:58
    

While second `print` gives me something like this:

    
    
    [datetime.datetime(2015, 2, 18, 10, 11, 58)]
    

Does anyone know how to print values from list like in first `print` instead
of this what I get from second one?


Answer: You are getting confused by the `str()` output of `datetime` objects and their
representation:

    
    
    >>> import datetime
    >>> dt = datetime.datetime(2015, 2, 18, 10, 11, 58)
    >>> dt
    datetime.datetime(2015, 2, 18, 10, 11, 58)
    >>> str(dt)
    '2015-02-18 10:11:58'
    >>> repr(dt)
    'datetime.datetime(2015, 2, 18, 10, 11, 58)'
    

`print` calls `str` on the objects you print, but lists and other containers
always show their contents with `repr()`.

You'll have to use some string formatting to process the list:

    
    
    print ', '.join(map(str, keys_latest_modified_date_list))
    

would print a comma-separated list of datetime objects converted to strings:

    
    
    >>> keys_latest_modified_date_list = [dt, dt]
    >>> keys_latest_modified_date_list
    [datetime.datetime(2015, 2, 18, 10, 11, 58), datetime.datetime(2015, 2, 18, 10, 11, 58)]
    >>> ', '.join(map(str, keys_latest_modified_date_list))
    '2015-02-18 10:11:58, 2015-02-18 10:11:58'
    >>> print ', '.join(map(str, keys_latest_modified_date_list))
    2015-02-18 10:11:58, 2015-02-18 10:11:58
    

