Created
November 23, 2012 03:29
-
-
Save r-plus/4133883 to your computer and use it in GitHub Desktop.
get first and last key from dictionary instance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| day23 = datetime.datetime(2012, 10, 23) | |
| day24 = datetime.datetime(2012, 10, 24) | |
| # dictionary does not have order. | |
| dict = {day24 : 24, day23 : 23} | |
| # get first key | |
| sorted(dict.keys())[0] | |
| # get last key | |
| sorted(dict.keys())[len(dict.keys()) - 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know if you know this but using
[-1]on line 11 would be equivalent to[len(dict.keys()) - 1]