Convert List of Strings to datetime in Python (2 Examples)

 

Hi! This short tutorial will show you how to turn a list of strings to datetime objects in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into Python code!

 

Import Modules

We will first need to import the built-in datetime and dateutil modules so that we can make use of their functions. Therefore, in your Python IDE, run the lines of code below:

from datetime import datetime
from dateutil.parser import parse

If running any of the above lines of code throws up an error, then you may have to install the modules, depending on the Python version you are using:

pip install datetime
pip install python-dateutil

 

Create Sample List of Strings

Next, we will create the sample list of strings that we will transform into datetime objects in this tutorial. So, run the code below to create the sample list of strings:

date_list = ["7/2/2018 02:30 PM", "6/8/2019 08:30 AM", "9/25/2020 11:15 AM"]
 
print(type(date_list))
# <class 'list'>

Now we have the list, date_list, which contains three strings, including some date and time information. Let’s now carry out some examples!

 

Example 1: Turn List of Strings to datetime Using List Comprehension & parse() Function

In this example, we will use list comprehension and the parse() function to turn the list of strings to datetime objects:

date_time = [parse(x) for x in date_list]
print(date_time)
 
# [datetime.datetime(2018, 7, 2, 14, 30), 
#  datetime.datetime(2019, 6, 8, 8, 30), 
#  datetime.datetime(2020, 9, 25, 11, 15)]
 
 
print(type(date_time))
 
# <class 'list'>

In the code above, we ran an iteration through the list of strings and then parsed the strings to the parse() function, which transformed the strings to datetime objects.
 

Example 2: Turn List of Strings to datetime Using For Loop & strptime() Function

In this next example, we will make use of Python for loop and the strptime() function to transform the list of strings to datetime:

format = "%m/%d/%Y %I:%M %p"
 
date_time = []
for date in date_list:
  date_time.append(datetime.strptime(date, format))
 
print(date_time)
 
# [datetime.datetime(2018, 7, 2, 14, 30), 
#  datetime.datetime(2019, 6, 8, 8, 30), 
#  datetime.datetime(2020, 9, 25, 11, 15)]
 
 
print(type(date_time))
 
# <class 'list'>

In the example, we first created a datetime format "%m/%d/%Y %I:%M %p", then we looped through the “date_list” object, and then we parsed the loop and the datetime format to the strptime() function, which replaced the strings with datetime objects, and was appended to the “date_time” list.

So that is how to convert a list of strings to datetime objects in Python. I hope you found these two examples helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to turn a list of strings into datetime objects in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to transform a list of strings to datetime objects in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list of strings to datetime objects in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top