Trying to run the following code in python 3.7:
date_list = [datetime.datetime.strptime(d['date'], '%Y-%m-%d') for d in date_settings and d['date'] != None]
This is the resulting error, can someone please clarify to me why this list comprehension cant see its own variable?
date_list = [datetime.datetime.strptime(d['date'], '%Y-%m-%d') for d in date_settings and d['date'] != None]
NameError: name 'd' is not defined
here is the inputed array:
[{'date': '2020-05-08', 'changed_at': '2020-05-07T20:35:07.854Z'}, {'date': '2020-05-09', 'changed_at': '2020-05-07T20:35:40.604Z'}, {'date': '2020-05-10', 'changed_at': '2020-05-07T20:35:42.936Z'}]
Inside your comprehension, where you wrote
for d in date_settings and d['date'] != None
you should have if
, not and
.
As you currently have it, the comprehension is trying to understand the expression
date_settings and d['date'] != None
as something that d
should iterate through, which is why it doesn't know what d
is supposed to mean here.