Sort dictionary chronologically by date in presence of None type
I am displaying the contents of a dictionary (a list of todos using todoist's API) on my Desktop.
I want them to appear in chronological order per their due date (and at the end of the list if no due date is set --> None
). The due date is written out in the following format in the dictionary : 'due_date_utc': 'Tue 13 Nov 2018 04:59:59 +0000',
and when no due date is set: 'due_date_utc': None,
.
Currently my code is:
rank = 0 # Simple counter to number the task (ie: 1-, 2-, 3-...)
for i in api.state['items']: # going through all the items in todoist
if i['checked'] == 0: # if the item is incomplete
rank += 1
print(rank, "- ", i['contents']) # Prints the list number and the task itself...
I have already looked at this post and that post but two main problems occur:
- They break when encountering the
None
type - They sort either by number or alphabetically instead of by date
I know I could setup a list environment for the different months of the year but I thought there might be a premade (and simpler solution) since the date format used by the API is pretty universal.
If your curious about the todoist API and what it contains, here is it's documentation page.
python dictionary todoist
add a comment |
I am displaying the contents of a dictionary (a list of todos using todoist's API) on my Desktop.
I want them to appear in chronological order per their due date (and at the end of the list if no due date is set --> None
). The due date is written out in the following format in the dictionary : 'due_date_utc': 'Tue 13 Nov 2018 04:59:59 +0000',
and when no due date is set: 'due_date_utc': None,
.
Currently my code is:
rank = 0 # Simple counter to number the task (ie: 1-, 2-, 3-...)
for i in api.state['items']: # going through all the items in todoist
if i['checked'] == 0: # if the item is incomplete
rank += 1
print(rank, "- ", i['contents']) # Prints the list number and the task itself...
I have already looked at this post and that post but two main problems occur:
- They break when encountering the
None
type - They sort either by number or alphabetically instead of by date
I know I could setup a list environment for the different months of the year but I thought there might be a premade (and simpler solution) since the date format used by the API is pretty universal.
If your curious about the todoist API and what it contains, here is it's documentation page.
python dictionary todoist
1
First, convert the dates todatetime
values; they sort easily from there. As forNone
values, what stops you from adding a simple check forval is not None
?
– Prune
Nov 16 at 0:12
add a comment |
I am displaying the contents of a dictionary (a list of todos using todoist's API) on my Desktop.
I want them to appear in chronological order per their due date (and at the end of the list if no due date is set --> None
). The due date is written out in the following format in the dictionary : 'due_date_utc': 'Tue 13 Nov 2018 04:59:59 +0000',
and when no due date is set: 'due_date_utc': None,
.
Currently my code is:
rank = 0 # Simple counter to number the task (ie: 1-, 2-, 3-...)
for i in api.state['items']: # going through all the items in todoist
if i['checked'] == 0: # if the item is incomplete
rank += 1
print(rank, "- ", i['contents']) # Prints the list number and the task itself...
I have already looked at this post and that post but two main problems occur:
- They break when encountering the
None
type - They sort either by number or alphabetically instead of by date
I know I could setup a list environment for the different months of the year but I thought there might be a premade (and simpler solution) since the date format used by the API is pretty universal.
If your curious about the todoist API and what it contains, here is it's documentation page.
python dictionary todoist
I am displaying the contents of a dictionary (a list of todos using todoist's API) on my Desktop.
I want them to appear in chronological order per their due date (and at the end of the list if no due date is set --> None
). The due date is written out in the following format in the dictionary : 'due_date_utc': 'Tue 13 Nov 2018 04:59:59 +0000',
and when no due date is set: 'due_date_utc': None,
.
Currently my code is:
rank = 0 # Simple counter to number the task (ie: 1-, 2-, 3-...)
for i in api.state['items']: # going through all the items in todoist
if i['checked'] == 0: # if the item is incomplete
rank += 1
print(rank, "- ", i['contents']) # Prints the list number and the task itself...
I have already looked at this post and that post but two main problems occur:
- They break when encountering the
None
type - They sort either by number or alphabetically instead of by date
I know I could setup a list environment for the different months of the year but I thought there might be a premade (and simpler solution) since the date format used by the API is pretty universal.
If your curious about the todoist API and what it contains, here is it's documentation page.
python dictionary todoist
python dictionary todoist
asked Nov 15 at 23:59
Benjamin Chausse
7017
7017
1
First, convert the dates todatetime
values; they sort easily from there. As forNone
values, what stops you from adding a simple check forval is not None
?
– Prune
Nov 16 at 0:12
add a comment |
1
First, convert the dates todatetime
values; they sort easily from there. As forNone
values, what stops you from adding a simple check forval is not None
?
– Prune
Nov 16 at 0:12
1
1
First, convert the dates to
datetime
values; they sort easily from there. As for None
values, what stops you from adding a simple check for val is not None
?– Prune
Nov 16 at 0:12
First, convert the dates to
datetime
values; they sort easily from there. As for None
values, what stops you from adding a simple check for val is not None
?– Prune
Nov 16 at 0:12
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329539%2fsort-dictionary-chronologically-by-date-in-presence-of-none-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329539%2fsort-dictionary-chronologically-by-date-in-presence-of-none-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
First, convert the dates to
datetime
values; they sort easily from there. As forNone
values, what stops you from adding a simple check forval is not None
?– Prune
Nov 16 at 0:12